首页 > 解决方案 > 在 Jacoco 的 jacocoTestReport.xml 文件中包含多个源集

问题描述

我在基于 Gradle 的项目中定义了一个自定义 sourceSet,如下所示:

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)

sourceSet 包含 Cucumber 功能。

运行 Jacoco 时,生成的jacocoTestReport.xml文件似乎不包括 Cucumber 测试生成的覆​​盖率。

这是 Jacoco 配置:

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

我能做些什么来在 xml 文件中获得与在 exec 文件中相同的覆盖率吗?

标签: gradlejacocojacoco-plugin

解决方案


经过进一步的实验,结果证明 xml 文件包含来自 componentTest sourceSet 的覆盖范围。

我问这个问题是因为 SonarQube 中的覆盖范围没有正确反映 - 我无法从 SonarQube 中的 componentTest 中看到 Cucumber 测试。但现在在我看来,实际的覆盖率值与 Jacoco 报告显示的一致。我只需要在 SonarQube 中排除与 Jacoco 中相同的源类。


推荐阅读