行业资讯 2025年08月6日
0 收藏 0 点赞 299 浏览 6715 个字
摘要 :

文章目录 1.介绍 2.JUnit XML报告的开放测试报告格式 2.1配置 2.2. XML报告 3.JUnit的旧格式XML报告 4.结论 在JUnit 5中,我们可以通过配置生成XML格式的测试执行报……




  • 1.介绍
  • 2.JUnit XML报告的开放测试报告格式
    • 2.1配置
    • 2.2. XML报告
  • 3.JUnit的旧格式XML报告
  • 4.结论

在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.zuozi.net/9545.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

988
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

465
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

348
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

456
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

517
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

832
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号