Search code examples
gradlejacocojacoco-plugin

Include multiple sourcesets in the Jacoco's jacocoTestReport.xml file


I have a custom sourceSet defined in a Gradle based project, like this:

sourceSets {
    componentTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/componentTest/java')
        }
    }
}

configurations {
    componentTestImplementation.extendsFrom testImplementation
}

task componentTest(type: Test) {
    dependsOn assemble
    testClassesDirs = sourceSets.componentTest.output.classesDirs
    classpath = sourceSets.componentTest.runtimeClasspath
    outputs.upToDateWhen { false }
    testLogging.showStandardStreams = true
    mustRunAfter(test)
}

check.dependsOn(componentTest)

The sourceSet contains Cucumber features.

When running Jacoco, the generated jacocoTestReport.xml file doesn't seem to include the coverage generated by the Cucumber tests.

Here is the Jacoco configuration:

jacocoTestReport {
    executionData(
            file("${project.buildDir}/jacoco/test.exec"),
            file("${project.buildDir}/jacoco/componentTest.exec"),
    )
}

Is there anything I could do to get the same coverage in the xml file as I get in the exec files?


Solution

  • After further experiments it turns out the xml file includes the coverage from the componentTest sourceSet.

    I asked this question because the coverage was not reflected correctly in SonarQube - I could not see the Cucumber tests from the componentTest in SonarQube. But it seems to me now, that the actual coverage values align with what the Jacoco report shows. I just had to exclude the same source classes in SonarQube as in Jacoco.