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


当前回答

“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报告之间的细微差异。

其他回答

关于使用python的好答案:(有很多方法可以做到这一点) Jenkins中的Python单元测试?

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

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

在jenkins构建配置中,添加一个“发布JUnit测试结果报告”操作,其中包含result.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报告之间的细微差异。

我在这方面找不到任何有用的信息,所以我做了一些反复试验。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-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格式规范的东西,因为JUnit不生成任何类型的XML或HTML报告。

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

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

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

希望它能帮助到一些人。