我有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>
其他回答
我在这方面找不到任何有用的信息,所以我做了一些反复试验。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子元素。
关于使用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和您生成的任何其他测试结果文件。
几个月前我做了类似的事情,结果证明这种简单的格式足以让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报告之间的细微差异。
推荐文章
- 如何使用GitHub Org Plugin从jenkins管道(jenkinsfile)触发另一个作业?
- Mockito的argumentCaptor的例子
- 在GitHub repo上显示Jenkins构建的当前状态
- 何时使用Mockito.verify()?
- Hudson支持的JUnit XML格式规范是什么?
- 比较JUnit断言中的数组,简洁的内置方式?
- Maven依赖项失败,出现501错误
- 用于双值的assertEquals的delta或epsilon参数的含义
- 我怎么能让詹金斯CI与Git触发器推到主人?
- Java的assertEquals方法可靠吗?
- IntelliJ IDEA with Junit 4.7”!!JUnit 3.8或更高版本:
- 更改参数化测试的名称
- 在执行JpaTest时无法找到@SpringBootConfiguration
- Java:如何测试调用System.exit()的方法?
- Junit @Rule如何工作?