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


当前回答

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

其他回答

几个月前我做了类似的事情,结果证明这种简单的格式足以让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”和“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和您生成的任何其他测试结果文件。

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

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

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

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

希望它能帮助到一些人。