为多项目构建中的多个Gradle项目提供平均代码覆盖率

bis0qfac  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(116)

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.

gmxoilav

gmxoilav1#

通过使用Gradle的--debug选项,我能够看到scoverage编译任务在测试执行之前没有运行。
所以,除了以上的改动,我还需要改动:

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

至:

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

相关问题