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


当前回答

基本结构 下面是一个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>

其他回答

几个月前我做了类似的事情,结果证明这种简单的格式足以让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-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子元素。

基本结构 下面是一个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 XML格式规范的东西,因为JUnit不生成任何类型的XML或HTML报告。

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

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

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

希望它能帮助到一些人。

“JUnit”和“xUnit”结果有多种模式。

XSD for Apache Ant的JUnit输出可以在https://github.com/windyroad/JUnit-Schema上找到(出处是https://stackoverflow.com/a/4926073/1733117) Jenkins xunit-plugin中的XSD可以在:https://github.com/jenkinsci/xunit-plugin/tree/master/src/main/resources/org/jenkinsci/plugins/xunit/types(在model/ XSD下)找到

请注意,Jenkins xunit-plugin使用的模式有几个版本(当前最新版本是junit-10)。xsd增加了对Erlang/OTP Junit格式的支持)。

一些测试框架以及“xUnit”风格的报告插件也使用它们自己的秘密武器来生成“xUnit”风格的报告,这些报告可能不使用特定的模式(请阅读:它们尝试使用,但工具可能不会针对任何一种模式进行验证)。Jenkins中的Python单元测试?快速比较了几个这些库以及生成的XML报告之间的细微差异。