ChatGPT解决这个技术问题 Extra ChatGPT

Hudson 支持的 JUnit XML 格式规范是什么?

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

有什么理由不使用 JUnit?这些测试可以通过各种工具 cmd、UI 等以各种方式自动化......
@AaronMcIver:Shell 脚本非常擅长在(非 Java 语言)上运行测试。你会如何使用 JUnit 呢?
@BenVoigt 我最初假设 OP 涉及 Java,并希望绕过 JUnit 作为测试工具。在查看问题后,很可能不是这种情况。看起来 code.google.com/p/shell2junit 可能会在第二次查看后为 OP 提供一些用处。
沿着 shell2unit 的思路,这里是我创建的可以解析/输出 JUnit XML 的 JAXB 类:gist.github.com/agentgt/8583649

C
Community

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

这个问题有更详细的答案:Spec. for JUnit XML Output


请对此答案进行更正,因为 xunit 插件拒绝“类名”属性并仅接受“类”
我有相反的问题。 class 被拒绝,只有 classname 有效。
当我将 xUnit 插件升级到 1.60 时,这对我来说开始失败。我发现验证器变得更严格了,我不得不添加 <testsuite tests="(number of tests)"> ex。 <testsuite tests="10">
感谢@KevinBrotcke,我将更新答案以包含该属性。
另请注意,要让 Hudson 按包/套件组织测试,您必须在 classname 属性中指定包。例如:<testcase classname="foo.bar" name="ATest" /> 这会将 bar 类放在 Jenkins 上的 foo 包中,从而使您的测试集合更有条理。
b
bdesham

我刚刚抓取了其他人已链接到的 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>

其中一些项目可能会多次出现:

只能有一个 testsuite 元素,因为这就是 XML 的工作方式,但 testsuites 元素中可以有多个 testsuite 元素。

每个属性元素可以有多个属性子元素。

每个测试套件元素可以有多个测试用例子项。

每个测试用例元素可以有多个错误、失败、系统输出或系统错误子项。

https://i.stack.imgur.com/CPp9v.png


是否有描述某些属性的有效值的文档,例如测试用例的状态或错误的类型?
@EricCope 我可以推荐查看源代码 svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/…。基本上它只是一个字符串。
为什么标签会重复?
设置镜像:imgur.com/quneFJf alt:RootelementtestsuitesMax recursive de...2Max Repeat factor2include optional elements:(是=勾选),include optional attributes:(是=已勾选)
@Nakilon 晚了 2.5 年,但我修好了
Y
Youngjae

问题 Anders Lindahltop answer 指的是 xsd file

就我个人而言,我发现 this xsd file 也非常有用(我不记得我是如何找到它的)。它看起来不那么令人生畏,而且就我使用它而言,所有元素和属性似乎都被 Jenkins (v1.451) 识别了

但有一件事:当添加多个 <failure ... 元素时,Jenkins 中只保留了一个。创建 xml 文件时,我现在将所有失败合并为一个。

2016 年 11 月更新该链接现已损坏。一个更好的选择是来自cubic.org 的这个页面:JUnit XML reporting file format,它已经做出了很大的努力来提供一个明智的记录示例。下面复制了示例和 xsd,但它们的页面看起来更好。

示例 JUnit XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- a description of the JUnit XML format and how Jenkins parses it. See also junit.xsd -->

<!-- if only a single testsuite element is present, the testsuites
     element can be omitted. All attributes are optional. -->
<testsuites disabled="" <!-- total number of disabled tests from all testsuites. -->
            errors=""   <!-- total number of tests with error result from all testsuites. -->
            failures="" <!-- total number of failed tests from all testsuites. -->
            name=""
            tests=""    <!-- total number of successful tests from all testsuites. -->
            time=""     <!-- time in seconds to execute all test suites. -->
        >

  <!-- testsuite can appear multiple times, if contained in a testsuites element.
       It can also be the root element. -->
  <testsuite name=""      <!-- Full (class) name of the test for non-aggregated testsuite documents.
                               Class name without the package for aggregated testsuites documents. Required -->
         tests=""     <!-- The total number of tests in the suite, required. -->
         disabled=""  <!-- the total number of disabled tests in the suite. optional -->
             errors=""    <!-- The total number of tests in the suite that errored. An errored test is one that had an unanticipated problem,
                               for example an unchecked throwable; or a problem with the implementation of the test. optional -->
             failures=""  <!-- The total number of tests in the suite that failed. A failure is a test which the code has explicitly failed
                               by using the mechanisms for that purpose. e.g., via an assertEquals. optional -->
             hostname=""  <!-- Host on which the tests were executed. 'localhost' should be used if the hostname cannot be determined. optional -->
         id=""        <!-- Starts at 0 for the first testsuite and is incremented by 1 for each following testsuite -->
         package=""   <!-- Derived from testsuite/@name in the non-aggregated documents. optional -->
         skipped=""   <!-- The total number of skipped tests. optional -->
         time=""      <!-- Time taken (in seconds) to execute the tests in the suite. optional -->
         timestamp="" <!-- when the test was executed in ISO 8601 format (2014-01-21T16:17:18). Timezone may not be specified. optional -->
         >

    <!-- Properties (e.g., environment settings) set during test
     execution. The properties element can appear 0 or once. -->
    <properties>
      <!-- property can appear multiple times. The name and value attributres are required. -->
      <property name="" value=""/>
    </properties>

    <!-- testcase can appear multiple times, see /testsuites/testsuite@tests -->
    <testcase name=""       <!-- Name of the test method, required. -->
          assertions="" <!-- number of assertions in the test case. optional -->
          classname=""  <!-- Full class name for the class the test method is in. required -->
          status=""
          time=""       <!-- Time taken (in seconds) to execute the test. optional -->
          >

      <!-- If the test was not executed or failed, you can specify one
           the skipped, error or failure elements. -->

      <!-- skipped can appear 0 or once. optional -->
      <skipped/>

      <!-- Indicates that the test errored. An errored test is one
           that had an unanticipated problem. For example an unchecked
           throwable or a problem with the implementation of the
           test. Contains as a text node relevant data for the error,
           for example a stack trace. optional -->
      <error message="" <!-- The error message. e.g., if a java exception is thrown, the return value of getMessage() -->
         type=""    <!-- The type of error that occured. e.g., if a java execption is thrown the full class name of the exception. -->
         ></error>

      <!-- Indicates that the test failed. A failure is a test which
       the code has explicitly failed by using the mechanisms for
       that purpose. For example via an assertEquals. Contains as
       a text node relevant data for the failure, e.g., a stack
       trace. optional -->
      <failure message="" <!-- The message specified in the assert. -->
           type=""    <!-- The type of the assert. -->
           ></failure>

      <!-- Data that was written to standard out while the test was executed. optional -->
      <system-out></system-out>

      <!-- Data that was written to standard error while the test was executed. optional -->
      <system-err></system-err>
    </testcase>

    <!-- Data that was written to standard out while the test suite was executed. optional -->
    <system-out></system-out>
    <!-- Data that was written to standard error while the test suite was executed. optional -->
    <system-err></system-err>
  </testsuite>
</testsuites>

JUnit XSD 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!-- from https://svn.jenkins-ci.org/trunk/hudson/dtkit/dtkit-format/dtkit-junit-model/src/main/resources/com/thalesgroup/dtkit/junit/model/xsd/junit-4.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="failure">
        <xs:complexType mixed="true">
            <xs:attribute name="type" type="xs:string" use="optional"/>
            <xs:attribute name="message" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="error">
        <xs:complexType mixed="true">
            <xs:attribute name="type" type="xs:string" use="optional"/>
            <xs:attribute name="message" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="properties">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="property" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="property">
        <xs:complexType>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="value" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="skipped" type="xs:string"/>
    <xs:element name="system-err" type="xs:string"/>
    <xs:element name="system-out" type="xs:string"/>

    <xs:element name="testcase">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="skipped" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="error" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="failure" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="system-out" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="system-err" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="assertions" type="xs:string" use="optional"/>
            <xs:attribute name="time" type="xs:string" use="optional"/>
            <xs:attribute name="classname" type="xs:string" use="optional"/>
            <xs:attribute name="status" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="testsuite">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="properties" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="testcase" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="system-out" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="system-err" minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute name="tests" type="xs:string" use="required"/>
            <xs:attribute name="failures" type="xs:string" use="optional"/>
            <xs:attribute name="errors" type="xs:string" use="optional"/>
            <xs:attribute name="time" type="xs:string" use="optional"/>
            <xs:attribute name="disabled" type="xs:string" use="optional"/>
            <xs:attribute name="skipped" type="xs:string" use="optional"/>
            <xs:attribute name="timestamp" type="xs:string" use="optional"/>
            <xs:attribute name="hostname" type="xs:string" use="optional"/>
            <xs:attribute name="id" type="xs:string" use="optional"/>
            <xs:attribute name="package" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="testsuites">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="testsuite" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="optional"/>
            <xs:attribute name="time" type="xs:string" use="optional"/>
            <xs:attribute name="tests" type="xs:string" use="optional"/>
            <xs:attribute name="failures" type="xs:string" use="optional"/>
            <xs:attribute name="disabled" type="xs:string" use="optional"/>
            <xs:attribute name="errors" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

你如何让失败看起来不错呢?我想手动添加换行符,但它们没有出现在 Jenkins 中。
这是我的方法的一个缺点。我记得在那口井里挣扎过。尝试添加类似 <br/> - 我忘记了这是如何解决的(我们不再使用它了),但这似乎值得一试。
我找到了解决方法。由于我们使用的是 c++,因此我只是在失败消息中报告失败次数,并使用“堆栈跟踪”来报告实际失败。由于堆栈跟踪是从带有失败元素正文的文本中报告的,因此可以正确支持新行。
I
Ian

我找不到任何关于此的好的信息,所以我做了一些试验和错误。 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>

(我从 this sample XML document 开始,然后从那里向后工作。)


更高版本的 Jenkins 插件可以处理 jUnit.XML 结果规范中的更多字段。
你能详细说明吗?
G
G. Demecki

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

首先:没有像 JUnit XML Format Specification 这样的东西,仅仅是因为 JUnit 不生成任何类型的 XML 或 HTML 报告。

XML 报告生成本身来自 Ant JUnit 任务/Maven Surefire 插件/Gradle(无论您使用哪个来运行测试)。 XML 报告格式首先由 Ant 引入,后来被 Maven(和 Gradle)改编。

如果有人只需要官方的 XML 格式,那么:

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

希望它会帮助某人。


谢谢你的澄清。我正在尝试send JUnit test summaries from an old Jenkins 1.6 instance to Slack - 也许你能帮忙?我会在哪里放置这样的 XML 文件?
@JJD 对不起,我不明白你。这样的 XML 文件到底是什么意思?但是我假设您已经使用 ant/maven/gradle 运行了 JUnit 测试,是吗?如果是,这些工具在测试执行后会生成漂亮的摘要报告。 Jenkins 的版本在这里无关紧要。
是的,我的构建通过 Gradle 运行。我想在使用 Jenkins 1.6 时将 JUnit 测试摘要发送到 Slack 频道。阅读 GitHub 讨论时,我想我必须在某个地方放置一个配置 XML 文件,让 Slack 插件获取测试摘要。也许我误解了。
请检查 Gradle 完成启动 JUnit 测试后是否存在正确生成的测试报告。然后 Slack 插件应该能够使用这些报告。
最后,您的建议将我推向了正确的方向:我必须配置正确的路径来查找 XML 文件。对于我的具有多种 Gradle 产品风格Android 项目,可以使用以下方法:**/build/test-results/**/TEST-*.xml。非常感谢!!!
Y
Youngjae

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

以下是典型 JUnit XML 报告的文档结构。请注意,报告可以包含 1 个或多个测试套件。每个测试套件都有一组属性(记录环境信息)。每个测试套件还包含 1 个或多个测试用例,如果测试未通过,每个测试用例将包含跳过、失败或错误节点。如果测试用例通过了,那么它将不包含任何节点。有关哪些属性对每个节点有效的更多详细信息,请参阅以下“模式”部分。

<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>

C
Community

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

Apache Ant 的 JUnit 输出的 XSD 可以在以下位置找到: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(在模型/xsd 下)

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

一些测试框架以及“xUnit”风格的报告插件也使用他们自己的秘诀来生成“xUnit”风格的报告,这些可能不使用特定的模式(请阅读:他们尝试但这些工具可能无法针对任何一个模式)。 Python unittests in Jenkins? 快速比较了其中几个库以及生成的 xml 报告之间的细微差别。


C
Community

关于使用 python 的好答案:(有很多方法可以做到)Python unittests in Jenkins?

恕我直言,最好的方法是编写 python unittest 测试并安装 pytest(类似于'yum install pytest')来安装 py.test。然后像这样运行测试:'py.test --junitxml results.xml test.py'。您可以运行任何 unittest python 脚本并获取 jUnit xml 结果。

https://docs.python.org/2.7/library/unittest.html

在 jenkins 构建配置 构建后操作 使用 result.xml 和您生成的任何更多测试结果文件添加“发布 JUnit 测试结果报告”操作。


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅