【问题标题】:Cross-module code coverage with Jacoco offline instrumentation in gradle mutlimodule projectgradle mutlimodule 项目中使用 Jacoco 离线检测的跨模块代码覆盖率
【发布时间】:2020-05-17 10:31:26
【问题描述】:

我必须在我的项目中使用 Jacoco 离线检测,因为也使用了 PowerMock。

问题描述:假设您的 gradle 项目包含两个模块:A、B。模块 A 的测试涵盖了模块 B 中的代码。在代码覆盖率数据收集中,我发现模块B 的覆盖率数据(应该由模块A 提供)被完全遗漏了。

我创建了一个演示该问题的测试项目:https://github.com/SurpSG/jacoco-offline-instrumentation

gradle 项目的 Jacoco 离线检测设置基于答案 https://stackoverflow.com/a/42238982/2689114

另一方面,当我使用 jacoco gradle 插件时,我可以观察到模块 A 为模块 B 提供的覆盖率数据已成功收集到摘要报告中。我又创建了一个测试项目来证明这一点:https://github.com/SurpSG/jacoco-gradle-plugin-merge-coverage

我对 gradle 多模块项目 + jacoco 离线检测的设置是否错误?

【问题讨论】:

    标签: java gradle jacoco


    【解决方案1】:

    经过一番调查,我发现 Gradle 中的模块依赖关系是通过 .jar 文件解决的:

    <dependent-module>.classpath contains <dependency-module>.jar
    

    因此,就我而言,我需要构建一些包含检测类的特殊 jar。

    仪表类

    task preprocessClassesForJacoco(dependsOn: ['classes']) {
            ext.outputDir = buildDir.path + '/classes-instrumented'
            doLast {
                ant.taskdef(name: 'instrument',
                        classname: 'org.jacoco.ant.InstrumentTask',
                        classpath: configurations.jacoco.asPath)
                ant.instrument(destdir: outputDir) {
                    fileset(dir: sourceSets.main.java.outputDir, includes: '**/*.class', erroronmissingdir: false)
                }
            }
        }
    

    下一步将是构建检测的 jar

    task jacocoInstrumentedJar(type: Jar, dependsOn: [preprocessClassesForJacoco]) {
        baseName "${project.name}-instrumented"
        from preprocessClassesForJacoco.outputDir // path to instrumented classes
    }
    

    最后,我们需要替换通常的.jar 用instrumented一个

    gradle.taskGraph.whenReady { graph ->
            if (graph.hasTask(preprocessClassesForJacoco)) {
                tasks.withType(Test) {
                    doFirst {
                        ...
                        // getting a module dependencies
                        def modulesDependencies = moduleDependencies(project)
                        // removing regular jars
                        classpath -= files(modulesDependencies.jar.outputs.files)
                        // adding instrumented jars
                        classpath += files(modulesDependencies.jacocoInstrumentedJar.outputs.files)
                    }
                }
            }
        }
    

    我已经使用上述步骤更新了示例项目https://github.com/SurpSG/jacoco-offline-instrumentation。随意查看该项目以进行尝试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多