如何为 JUnit 5 测试创建 HTML 报告? [英] How to create an HTML report for JUnit 5 tests?

查看:37
本文介绍了如何为 JUnit 5 测试创建 HTML 报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当通过 Gradle 启动 JUnit 测试时,是否已经有可能生成 HTML 报告?任何提示或评论表示赞赏.

Is there already a possibility to generate an HTML report when JUnit tests were started via Gradle? Any hint or comment is appreciated.

推荐答案

UPDATE

Gradle 4.6 为 JUnit 平台提供内置支持em> 允许您使用标准 Gradle test 任务运行 JUnit Jupiter 测试,该任务可生成开箱即用的 HTML 报告.

Gradle 4.6 provides built-in support for the JUnit Platform which allows you to run JUnit Jupiter tests using the standard Gradle test task which generates HTML reports out of the box.

4.6 之前的 Gradle 版本的答案

JUnit 平台 Gradle 插件生成 JUnit 4 风格的 XML 测试报告.

The JUnit Platform Gradle Plugin generates JUnit 4 style XML test reports.

这些 XML 文件默认输出到 build/test-results/junit-platform.

These XML files are output to build/test-results/junit-platform by default.

因此,如果您的构建服务器知道如何解析 JUnit 4 样式的 XML 报告,您只需将其指向该目录中的 XML 文件,然后让构建服务器为您生成 HTML 报告即可.

So, if your build server knows how to parse JUnit 4 style XML reports, you can just point it to the XML files in that directory and let the build server generate the HTML report for you.

但是,如果您问 Gradle 是否可以为通过 junitPlatformTest 任务运行的测试生成 HTML 报告,那么答案是不,不幸的是不能".原因是标准 Gradle test 任务仅根据其专有的二进制"报告格式生成 HTML 报告.由于 junitPlatformTest 任务不会以 Gradle 的二进制格式生成报告,因此 Gradle 本身无法为 JUnit 平台测试生成 HTML 报告.

However, if you are asking if Gradle can generate an HTML report for your tests run via the junitPlatformTest task, then the answer is "No, unfortunately not." The reason is that the standard Gradle test task only generates HTML reports based on its own proprietary "binary" report format. Since the junitPlatformTest task does not generate reports in Gradle's binary format, Gradle itself cannot generate HTML reports for JUnit Platform tests.

话虽如此,但实际上有一个变通方法:您可以在您的 Gradle 构建中使用 Ant.Ant 有一个任务,用于聚合基于 JUnit 4 的 XML 报告并从这些聚合报告生成 HTML 报告.输出不是很现代,但至少是人类可读的.缺点是默认的 XSLT 样式表不显示通过 JUnit 平台运行的测试的测试类名称.

Having said that, however, there is in fact a work around: you can use Ant within your Gradle build. Ant has a task for aggregating JUnit 4 based XML reports and generating an HTML report from those aggregated reports. The output is not very modern, but it is at least human readable. The downside is that the default XSLT stylesheet does not display the test class names for tests run via the JUnit Platform.

在任何情况下,您都可以在 Gradle 中配置 Ant 的 JUnitReport 任务如下.

In any case, you can configure Ant's JUnitReport task in Gradle as follows.

junitPlatform {
    // configure as normal
}

configurations {
    junitXmlToHtml
}

task generateHtmlTestReports << {
    def reportsDir = new File(buildDir, 'test-reports')
    reportsDir.mkdirs()

    ant.taskdef(
        name: 'junitReport',
        classname: 'org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator',
        classpath: configurations.junitXmlToHtml.asPath
    )

    ant.junitReport(todir: "$buildDir/test-results/junit-platform", tofile: "aggregated-test-results.xml") {
        fileset(dir: "$buildDir/test-results/junit-platform")
        report(format: 'frames', todir: reportsDir)
    }
}

afterEvaluate {
    def junitPlatformTestTask = tasks.getByName('junitPlatformTest')
    generateHtmlTestReports.dependsOn(junitPlatformTestTask)
    check.dependsOn(generateHtmlTestReports)
}

dependencies {
    // configure as normal ...

    junitXmlToHtml 'org.apache.ant:ant-junit:1.9.7'
}

然后,执行gradle check会在build/test-reports/index.html中生成一个HTML报告.

Then, executing gradle check will generate an HTML report in build/test-reports/index.html.

问候,

Sam(核心 JUnit 5 提交者)

这篇关于如何为 JUnit 5 测试创建 HTML 报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆