我有Hudson作为持续集成服务器,我想使用选项“发布JUnit测试结果报告”。但是我不使用xUnit工具进行测试,相反,我有运行测试并以简单格式返回结果的shell脚本。我正在考虑制作一个脚本,将这些结果转换为JUnit格式。所以我很有趣的JUnit文件必须看?


当前回答

我在这方面找不到任何有用的信息,所以我做了一些反复试验。Jenkins (v1.585)可以识别以下属性和字段(而且只有这些属性和字段)。

<?xml version="1.0" encoding="UTF-8"?>
<testsuite>

  <!-- if your classname does not include a dot, the package defaults to "(root)" -->
  <testcase name="my testcase" classname="my package.my classname" time="29">

    <!-- If the test didn't pass, specify ONE of the following 3 cases -->

    <!-- option 1 --> <skipped />
    <!-- option 2 --> <failure message="my failure message">my stack trace</failure>
    <!-- option 3 --> <error message="my error message">my crash report</error>

    <system-out>my STDOUT dump</system-out>

    <system-err>my STDERR dump</system-err>

  </testcase>

</testsuite>

(我从这个示例XML文档开始,并从那里向后进行。)

其他回答

基本结构 下面是一个JUnit输出文件的示例,显示了跳过和失败的结果,以及单个通过的结果。

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
   <testsuite name="JUnitXmlReporter" errors="0" tests="0" failures="0" time="0" timestamp="2013-05-24T10:23:58" />
   <testsuite name="JUnitXmlReporter.constructor" errors="0" skipped="1" tests="3" failures="1" time="0.006" timestamp="2013-05-24T10:23:58">
      <properties>
         <property name="java.vendor" value="Sun Microsystems Inc." />
         <property name="compiler.debug" value="on" />
         <property name="project.jdk.classpath" value="jdk.classpath.1.6" />
      </properties>
      <testcase classname="JUnitXmlReporter.constructor" name="should default path to an empty string" time="0.006">
         <failure message="test failure">Assertion failed</failure>
      </testcase>
      <testcase classname="JUnitXmlReporter.constructor" name="should default consolidate to true" time="0">
         <skipped />
      </testcase>
      <testcase classname="JUnitXmlReporter.constructor" name="should default useDotNotation to true" time="0" />
   </testsuite>
</testsuites>

Below is the documented structure of a typical JUnit XML report. Notice that a report can contain 1 or more test suite. Each test suite has a set of properties (recording environment information). Each test suite also contains 1 or more test case and each test case will either contain a skipped, failure or error node if the test did not pass. If the test case has passed, then it will not contain any nodes. For more details of which attributes are valid for each node please consult the following section "Schema".

<testsuites>        => the aggregated result of all junit testfiles
  <testsuite>       => the output from a single TestSuite
    <properties>    => the defined properties at test execution
      <property>    => name/value pair for a single property
      ...
    </properties>
    <error></error> => optional information, in place of a test case - normally if the tests in the suite could not be found etc.
    <testcase>      => the results from executing a test method
      <system-out>  => data written to System.out during the test run
      <system-err>  => data written to System.err during the test run
      <skipped/>    => test was skipped
      <failure>     => test failed
      <error>       => test encountered an error
    </testcase>
    ...
  </testsuite>
  ...
</testsuites>

我刚拿了junit-4。xsd,其他人已经链接到它,并使用名为XMLSpear的工具使用如下所示的选项将模式转换为空白XML文件。这是(稍微清理了一下)结果:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites disabled="" errors="" failures="" name="" tests="" time="">
    <testsuite disabled="" errors="" failures="" hostname="" id=""
               name="" package="" skipped="" tests="" time="" timestamp="">
        <properties>
            <property name="" value=""/>
        </properties>
        <testcase assertions="" classname="" name="" status="" time="">
            <skipped/>
            <error message="" type=""/>
            <failure message="" type=""/>
            <system-out/>
            <system-err/>
        </testcase>
        <system-out/>
        <system-err/>
    </testsuite>
</testsuites>

有些项目可能会出现多次:

只能有一个testsuites元素,因为这是XML的工作方式,但是在testsuites元素中可以有多个testsuite元素。 每个属性元素可以有多个属性子元素。 每个测试套件元素可以有多个测试用例子元素。 每个测试用例元素可以有多个error、failure、system-out或system-err子元素。

我在这方面找不到任何有用的信息,所以我做了一些反复试验。Jenkins (v1.585)可以识别以下属性和字段(而且只有这些属性和字段)。

<?xml version="1.0" encoding="UTF-8"?>
<testsuite>

  <!-- if your classname does not include a dot, the package defaults to "(root)" -->
  <testcase name="my testcase" classname="my package.my classname" time="29">

    <!-- If the test didn't pass, specify ONE of the following 3 cases -->

    <!-- option 1 --> <skipped />
    <!-- option 2 --> <failure message="my failure message">my stack trace</failure>
    <!-- option 3 --> <error message="my error message">my crash report</error>

    <system-out>my STDOUT dump</system-out>

    <system-err>my STDERR dump</system-err>

  </testcase>

</testsuite>

(我从这个示例XML文档开始,并从那里向后进行。)

几个月前我做了类似的事情,结果证明这种简单的格式足以让Hudson接受它作为测试协议:

<testsuite tests="3">
    <testcase classname="foo1" name="ASuccessfulTest"/>
    <testcase classname="foo2" name="AnotherSuccessfulTest"/>
    <testcase classname="foo3" name="AFailingTest">
        <failure type="NotEnoughFoo"> details about failure </failure>
    </testcase>
</testsuite>

这个问题有更详细的答案:JUnit XML输出规范

我决定发布一个新的答案,因为一些现有的答案已经过时或不完整。

首先:没有类似JUnit XML格式规范的东西,因为JUnit不生成任何类型的XML或HTML报告。

XML报告生成本身来自Ant JUnit任务/ Maven Surefire Plugin/ Gradle(您用于运行测试的任何一个)。XML报告格式最初由Ant引入,后来由Maven(和Gradle)进行了改编。

如果有人只是需要一个正式的XML格式,那么:

存在一个maven surefire生成的XML报告的模式,可以在这里找到:surefire-test-report.xsd。 对于蚂蚁生成的XML,这里有一个第三方模式可用(但它可能有点过时)。

希望它能帮助到一些人。