使用JaCoCo获得项目范围的覆盖范围

时间:2016-07-20 19:30:18

标签: android gradle code-coverage jacoco

我有一个包含两个模块的项目,一个包含代码(submodule1),另一个包含单元测试(submodule2)。在submodule1上运行覆盖测试会提供0%的覆盖率结果,因为这些方法应该在submodule2中进行测试。然而,在submodule2上运行覆盖测试仅返回该模块中定义的类的覆盖范围。

我一直在努力创建项目范围的覆盖范围,我发现了以下内容:

subprojects {
    apply plugin: 'jacoco'

    def coverageSourceDirs = [
        'src/main/java'
    ]

    jacocoTestReport {
        additionalSourceDirs = files(sourceSets.main.allSource.srcDirs)
        sourceDirectories = files(sourceSets.main.allSource.srcDirs)
        classDirectories =  files(sourceSets.main.output)
        reports {
            html.enabled = true
            xml.enabled = true
            csv.enabled = false
        }
    }

    test.finalizedBy(project.tasks.jacocoTestReport)
}

task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
    additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
    sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories = files(subprojects.sourceSets.main.output)

    dependsOn = subprojects.test

    executionData = files(subprojects.jacocoTestReport.executionData)

    reports {
        html.enabled = true
        xml.enabled = true
    }

    onlyIf = {
        true
    }

    doFirst {
        executionData = files(executionData.findAll {
            it.exists()
        })
    }
}

但是我无法运行它,因为我在尝试运行./gradlew jacocoRootReport时出现此错误:

Could not find method jacocoTestReport() for arguments [build_6nnhcgikryugeat1c382yp5m6$_run_closure3$_closure6@1a3e8e24] on root project 'MyProject'.

我该如何解决这个问题?

0 个答案:

没有答案