How to generate an aggregated test report for all Gradle subprojects

Jakub Zalas - Jul 8 '20 - - Dev Community

Note: This article is outdated. The Test Report Aggregation Plugin can be used to achieve the same result with less effort.

With the plugin it all boils down to the example below.

With gradle:

plugins {
    id 'test-report-aggregation'
}

tasks.named('check') {
    dependsOn tasks.named('testAggregateTestReport', TestReport)
}
Enter fullscreen mode Exit fullscreen mode

With Kotlin:

plugins {
    id("test-report-aggregation")
}

tasks.check {
    dependsOn(tasks.named<TestReport>("testAggregateTestReport")) 
}
Enter fullscreen mode Exit fullscreen mode

Read more about the plugin in the official documentation: https://docs.gradle.org/current/userguide/test_report_aggregation_plugin.html


When using Gradle's multi-project builds, test reports are generated separately in each build directory of every sub-project.

To make my CI server's life easier I often like to aggregate reports in a single build directory:

// build.gradle.kts

plugins {
    base
    kotlin("jvm") version "1.3.72" apply false
}

allprojects {
    group = "com.kaffeinelabs"
    version = "1.0.0-SNAPSHOT"
}

val testReport = tasks.register<TestReport>("testReport") {
    destinationDir = file("$buildDir/reports/tests/test")
    reportOn(subprojects.mapNotNull {
        it.tasks.findByPath("test") 
    })

subprojects {
    tasks.withType<Test> {
        useJUnitPlatform()
        finalizedBy(testReport)
        testLogging {
            events("passed", "skipped", "failed")
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

If you're interested in how to aggregate code coverage reports, I covered it in another article:

. . . . . . . . . . . . .