gradlescoverage

Contribute scoverage code coverage to multiple Gradle projects in a multi-project build


I am using gradle-scoverage with a multi-project Gradle build. I want to make use of the fact that :projectB's tests exercise code in :projectA. But gradle-scoverage 7.0.0 does not show any code coverage for the relevant line in :projectA in the coverage report for :projectB, and the coverage report for :projectA is only for that project.

I found a proposed solution in the GitHub issue tracker:

project(':projectA').tasks.reportScoverage.dependsOn(project(':projectB').tasks.reportScoverage)

but it didn't work. The first problem was that the tasks hadn't been created at the time of executing the top-level project's build.gradle file, because the gradle-scoverage plugin is applied to each Gradle project's build.gradle file separately.

So I tried moving that line to projectB's build.gradle file, and changing it to read:

project(':projectA').tasks.reportScoverage.dependsOn(tasks.reportScoverage)

But then the dependency relationship didn't work, because while the reportScoverage tasks executed in the desired order, the reportTestScoverage tasks executed in the opposite order. So I changed it to:

project(':projectA').tasks.reportTestScoverage.dependsOn(tasks.reportTestScoverage)

But then Gradle again complained that it couldn't find the reportTestScoverage task.

So, following the advice here, I got it building again with:

project(':projectA').tasks.reportTestScoverage.dependsOn(':projectB:reportTestScoverage')

But although now projectB's reportTestScoverage task runs first, still no code coverage is shown for the relevant line in projectA. I have verified that the line is hit by running the relevant test in projectB in the IntelliJ debugger.

Then I realised I had forgotten to add:

scoverage project(path: ':projectA', configuration: 'scoverage')

to projectB's dependencies, as mentioned in the GitHub issue comment. But adding this made no difference.

I ran gradlew clean build after each attempt.

I wasn't able to debug scoverage's bytecode enhancement using IntelliJ, but by using jdb, I can see that when running projectB's tests, the class where the relevant line is showing no coverage, has not been instrumented by scoverage.


Solution

  • By using the --debug option to Gradle, I was able to see that the scoverage compilation task wasn't being run before the test execution.

    So, in addition to the above changes, I needed to change:

    scoverage project(path: ':projectA', configuration: 'scoverage')
    

    to:

    scoverage project(':projectA').sourceSets.scoverage.output