JUnit 5 如何生成XML测试执行报告

后端 潘老师 6个月前 (10-24) 164 ℃ (0) 扫码查看

在JUnit 5中,我们可以通过配置生成XML格式的测试执行报告,以便更好地了解测试执行的情况。本教程将介绍如何在JUnit 5中创建XML报告,包括新支持的开放测试报告格式和传统的格式,以便向后兼容。

1.介绍

默认情况下,JUnit的junit-platform-reporting构件使用一个TestExecutionListener的实现,生成包含测试执行摘要的XML报告。开发人员和第三方工具可以使用此报告以各种格式创建测试执行报告。

目前,JUnit 5支持两种XML报告格式:

  • 传统报告格式
  • 开放测试报告格式(从JUnit 5.9.x开始添加)

开放测试报告格式是一组新的测试报告格式、与测试框架无关和编程语言。目前有两种格式在规范中定义:

  • 基于事件的格式,适合将事件写入文件,并通过本地套接字或网络连接传输事件。JUnit 5使用这种格式。
  • 与现有的分层测试结果表示相似,该格式通过树形结构展示测试及其执行结果。传统的JUnit报告采用这种格式。

2.JUnit XML报告的开放测试报告格式

2.1配置

要创建新格式的测试执行报告,请确保项目中使用了最新版本的junit-platform-reporting依赖项。

pom.xml:

<dependency>
  <groupId>org.junit.platform</groupId>
  <artifactId>junit-platform-reporting</artifactId>
  <version>1.9.1</version>
  <scope>test</scope>
</dependency>

build.gradle:

dependencies {
    testRuntimeOnly("org.junit.platform:junit-platform-reporting:1.9.1")
}

接下来,我们需要传递以下参数来启用新格式或传统格式的报告。第一个参数用于启用/禁用报告的写入。第二个参数用于配置输出目录。

默认情况下,如果找到Gradle构建脚本,输出目录设置为’/build’ ‘;如果找到Maven POM文件,输出目录设置为’/target’ ‘;否则,使用当前工作目录。

  • junit.platform.reporting.open.xml.enabled = true/false
  • junit.platform.reporting.output.dir =<path>

以下是构建系统的示例配置条目:

pom.xml:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <properties>
                    <configurationParameters>
                        junit.platform.reporting.open.xml.enabled = true
                        junit.platform.reporting.output.dir = target/surefire-reports
                    </configurationParameters>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>

build.gradle:

tasks.withType(Test).configureEach {
    def outputDir = reports.junitXml.outputLocation
    jvmArgumentProviders << ({
        [
            "-Djunit.platform.reporting.open.xml.enabled=true",
            "-Djunit.platform.reporting.output.dir=${outputDir.get().asFile.absolutePath}"
        ]
    } as CommandLineArgumentProvider)
}

2.2. XML报告

现在,当我们运行’mvn test’命令时,OpenTestReportGeneratingListener会为每次配置的输出目录中的测试运行创建一个名为junit-platform-events-random_id.xml的XML报告文件。

为了演示,让我们运行以下包含两个测试的测试类。第一个测试成功,第二个测试失败。

public class HelloTest {
  @Test
  void testOne(){
    Assertions.assertTrue(true);
  }
  @Test
  void testTwo(){
    Assertions.assertTrue(false);
  }
}

从控制台运行测试。

$ mvn test

并查看生成的XML报告文件名为junit-platform-events-3100202407917663308.xml。

<?xml version="1.0" ?>
<e:events ...>
<infrastructure>
....
</infrastructure>
<e:started id="1" name="JUnit Jupiter" time="2022-10-07T10:57:29.193634500Z">
<metadata>
<junit:uniqueId>[engine:junit-jupiter]</junit:uniqueId>
<junit:legacyReportingName>JUnit Jupiter</junit:legacyReportingName>
<junit:type>CONTAINER</junit:type>
</metadata>
</e:started>
<e:started id="2" name="HelloTest" parentId="1" time="2022-10-07T10:57:29.247574500Z">
<metadata>
<junit:uniqueId>[engine:junit-jupiter]/[class:com.howtodoinjava.junit5.examples.xmlReport.HelloTest]</junit:uniqueId>
<junit:legacyReportingName>com.howtodoinjava.junit5.examples.xmlReport.HelloTest</junit:legacyReportingName>
<junit:type>CONTAINER</junit:type>
</metadata>
<sources>
<java:classSource className="com.howtodoinjava.junit5.examples.xmlReport.HelloTest"/>
</sources>
</e:started>
<e:started id="3" name="testOne()" parentId="2" time="2022-10-07T10:57:29.269523200Z">
<metadata>
<junit:uniqueId>[engine:junit-jupiter]/[class:com.howtodoinjava.junit5.examples.xmlReport.HelloTest]/[method:testOne()]</junit:uniqueId>
<junit:legacyReportingName>testOne()</junit:legacyReportingName>
<junit:type>TEST</junit:type>
</metadata>
<sources>
<java:methodSource className="com.howtodoinjava.junit5.examples.xmlReport.HelloTest" methodName="testOne" methodParameterTypes=""/>
</sources>
</e:started>
<e:finished id="3" time="2022-10-07T10:57:29.307527100Z">
<result status="SUCCESSFUL"/>
</e:finished>
<e:started id="4" name="testTwo()" parentId="2" time="2022-10-07T10:57:29.312537Z">
<metadata>
<junit:uniqueId>[engine:junit-jupiter]/[class:com.howtodoinjava.junit5.examples.xmlReport.HelloTest]/[method:testTwo()]</junit:uniqueId>
<junit:legacyReportingName>testTwo()</junit:legacyReportingName>
<junit:type>TEST</junit:type>
</metadata>
<sources>
<java:methodSource className="com.howtodoinjava.junit5.examples.xmlReport.HelloTest" methodName="testTwo" methodParameterTypes=""/>
</sources>
</e:started>
<e:finished id="4" time="2022-10-07T10:57:29.317529600Z">
<result status="FAILED">
<java:throwable assertionError="true" type="org.opentest4j.AssertionFailedError">
<![CDATA[org.opentest4j.AssertionFailedError: expected: <true>but was: <false>at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151) ...]]>
</java:throwable>
</result>
</e:finished>
<e:finished id="2" time="2022-10-07T10:57:29.325526800Z">
<result status="SUCCESSFUL"/>
</e:finished>
<e:finished id="1" time="2022-10-07T10:57:29.331528600Z">
<result status="SUCCESSFUL"/>
</e:finished>
<e:started id="5" name="JUnit Platform Suite" time="2022-10-07T10:57:29.332527800Z">
<metadata>
<junit:uniqueId>[engine:junit-platform-suite]</junit:uniqueId>
<junit:legacyReportingName>JUnit Platform Suite</junit:legacyReportingName>
<junit:type>CONTAINER</junit:type>
</metadata>
</e:started>
<e:finished id="5" time="2022-10-07T10:57:29.333523400Z">
<result status="SUCCESSFUL"/>
</e:finished>
<e:started id="6" name="JUnit Vintage" time="2022-10-07T10:57:29.333523400Z">
<metadata>
<junit:uniqueId>[engine:junit-vintage]</junit:uniqueId>
<junit:legacyReportingName>JUnit Vintage</junit:legacyReportingName>
<junit:type>CONTAINER</junit:type>
</metadata>
</e:started>
<e:finished id="6" time="2022-10-07T10:57:29.335541600Z">
<result status="SUCCESSFUL"/>
</e:finished>
</e:events>

3.JUnit的旧格式XML报告

生成旧格式XML报告的最简单方法是设置junit.platform.reporting.open.xml.enabled参数为false。

junit.platform.reporting.open.xml.enabled = false

当我们重新运行测试时,LegacyXmlReportGeneratingListener会以以下格式生成默认报告,名称为TEST-com.howtodoinjava.junit5.examples.xmlReport.HelloTest.xml。

Legacy report format<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" failures="1" name="com.howtodoinjava.junit5.examples.xmlReport.HelloTest"
skipped="0" tests="2" time="0.076" version="3.0" ...>
<properties>
<property name="java.specification.version" value="17"/>
<property name="sun.cpu.isalist" value="amd64"/>
<property name="sun.jnu.encoding" value="Cp1252"/>
<property name="java.class.path" value="..."/>
<property name="java.vm.vendor" value="Oracle Corporation"/>
...
</properties>
<testcase classname="com.howtodoinjava.junit5.examples.xmlReport.HelloTest" name="testOne" time="0.04"/>
<testcase classname="com.howtodoinjava.junit5.examples.xmlReport.HelloTest" name="testTwo" time="0.008">
<failure message="expected: &lt;true&gt; but was: &lt;false&gt;" type="org.opentest4j.AssertionFailedError">
<![CDATA[org.opentest4j.AssertionFailedError: expected: <true>but was: <false>at
org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)...]]>
</failure>
</testcase>
</testsuite>

4.结论

这个简短的教程教会了我们JUnit的XML报告生成,包括旧的和新的开放测试报告格式。此外,我们还学会了如何使用配置参数切换特定版本,包括报告的输出目录位置。


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/back/10316.html
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】