Kotest JUnit XML Format Reporter

Kotest can generate test reports in JUnit XML format. This format can be used in automated test execution environments such as CI/CD tools. JUnit XML reports record test execution results and are used to analyze them and generate statistics and quality metrics for test results.

JUnit includes an XML report generator called the legacy XML report. Many tools integrate with this format, so it is very useful. However, this report has no concept of nested tests. Therefore, when used with nested testing styles in Kotest, parent tests are included as orphans.

To solve this problem, Kotest has its own implementation of the same format, and you can configure whether to include parent tests or collapse names.

To configure it in your project, add JunitXmlReporter using project configuration.

class MyConfig : AbstractProjectConfig() {
  override fun extensions(): List<Extension> = listOf(
    JunitXmlReporter(
      includeContainers = false, // don't write out status for all tests
      useTestPathAsName = true, // use the full test path (ie, includes parent test names)
      outputDir = "../target/junit-xml" // include to set output dir for maven
    )
  )
}

The reporter also needs to know where the build output folder is by setting a system property. Gradle also needs to know that it should not generate its own JUnit XML report. Configure this in Gradle’s test block.

tasks.named<Test>("test") {
  useJUnitPlatform()
  reports {
    junitXml.required.set(false)
  }
  systemProperty("gradle.build.dir", project.buildDir)
}

Parameters

The reporter has three parameters:

  • If includeContainers is true, all intermediate tests are included in the report as their own tests. The default is false.
  • If useTestPathAsName is true, the full test path is used as the name. In other words, the names of all parent tests are included in the name as a single string.
  • If outputDir is set, reports are generated in that folder. The default is test-results/test.

References