Aggregating Junit5 XML report

Hello Team !
I have multi-module, multi-language repo which I am scanning using SonarScanner 4.3. On Sonar dashboard, I am not able to show unit test count of my Junit5 test.

As per my debugging, understood that I have to provide Junit XML report to sonar.junit.reportPaths= property to show the count.

I do not want to provide hard coded value to this property. So option which I am exploring is if somehow if I can aggregate all the Junit5 XML report under repository at root project level then I can just point the sonar.junit.reportPaths= property to that location.

My Java build is via Gradle and I do see following option which is for HTML report and not for XML reports.

task junit5TestReport(type: TestReport) {    
    destinationDir = file("$buildDir/reports/tests/test")    
    // Include the results from the `test` task in all subprojects
    reportOn subprojects*.test
}
test.finalizedBy(junit5TestReport)

Any way to get similar thing for XML reports?

After some reading, I deployed a simple solution of gradle copying the required files from all subproject to root project directory.

task junit5TestReport(type: Copy) {
    from subprojects*.test
    into './build/reports/test'
    include '*.xml'
}
test.finalizedBy junit5TestReport
1 Like

@anupchandak thanks for sharing! Here is the Kotlin DSL version:

tasks.register<Copy>("testCollectjUnitXmlReports") {
    val allTestTasks = rootProject
        .subprojects
        .flatMap { it.tasks.withType<Test>() }
    val allJunitXmlLocations = allTestTasks
        .map { it.reports.junitXml.outputLocation.asFile.get() }
    from(*allJunitXmlLocations.toTypedArray())
    into(rootProject.layout.buildDirectory.dir("test-results/test"))
    include("*.xml")

    mustRunAfter(*allTestTasks.toTypedArray())
}

Well, copying the files from other projects tasks like this is unsafe and very bad and shouldn’t be done.
If you really need the files collected, you should use safe publication between projects using configurations selected by attributes. Actually the test-report-aggregation plugin does exactly that, so you could just apply it and use the configuration it added to get all the result files.

But actually specifically for the case OP asked about, it is not necessary at all.
The Sonar Gradle scanner defines the repot paths per project of the multi-project build and then does the aggregation itself, no need to copy together anything or to configure all paths in one place.