How to create an HTML report for JUnit 5 tests?
Asked Answered
M

4

15

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

Mile answered 12/9, 2016 at 7:1 Comment(0)
S
17

UPDATE

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.


Answer for Gradle versions prior to 4.6

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

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

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.

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.

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.

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'
}

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

Regards,

Sam (Core JUnit 5 committer)

Shamefaced answered 12/9, 2016 at 17:13 Comment(5)
So, if I understand it correctly, this is due to changes between JUnit4 and JUnit5, right?Tysontyumen
Yes. The JUnit Platform (part of "JUnit 5") provides more reporting information than JUnit 4 did and therefore does not map onto the built-in test infrastructure in Gradle (which is based on JUnit 4).Shamefaced
You can watch the following issue for updates on official JUnit Platform support from the Gradle team: github.com/gradle/gradle/issues/828Shamefaced
worth noting that this snippet will not generate reports for a failing test. To do this you will need to run ./gradlew --continue check generateHtmlTestReports or similarBestraddle
Important, if you had the junit 5 gradle plugin that comes with junit 5, enabled before 4.6, you need to remove config for that. Took me a while to figure that out. We had org.junit.platform:junit-platform-gradle-plugin:1.0.1 applied. No longer needed as of 4.6Inotropic
A
2

Adding below line to my java command created TEST-junit-jupiter.xml in my target/test-result folder. This xml file has all info about number of testcases run, number of tests passed/failed etc

 --reports-dir target/test-result
Alfredoalfresco answered 21/3, 2018 at 4:53 Comment(0)
P
1

The test report will be available in, by default, build/reports/tests/<Task Name>/index.html.

You can change that like so in the build.gradle[.kts] file:

reporting.baseDir = file("my-reports")

See https://docs.gradle.org/current/userguide/java_testing.html#test_reporting

Peeved answered 22/9, 2023 at 6:25 Comment(0)
D
-2

Yes, you can using Jacoco plugin.

Here is an example:

apply plugin: 'war' or apply plugin: 'java'
apply plugin: "jacoco"

test {
    reports.junitXml.destination="build/test-results"
    jacoco {
        destinationFile = file("build/jacoco/jacoco.exec")
        append=true
    }
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

Regards.

Disk answered 12/9, 2016 at 13:52 Comment(1)
Jacoco creates a coverage report, different to a test success report!Oxidation

© 2022 - 2024 — McMap. All rights reserved.