Gradle Jacoco 报告覆盖百分比与验证百分比不匹配

Gradle Jacoco report coverage percent doesn't match verification percent

我们配置排除:

def coverageExcludes() {
    return [
        "com/ourcompany/*Config*",
        "com/ourcompany/ServiceApplication*",
        "com/ourcompany/Shutdown*",
        "com/ourcompany/ShutdownConnector*",
        "com/ourcompany/Tomcat*",
        "com/ourcompany/TomcatCustomConnectorCustomizer*",
        "com/ourcompany/endpoint/exception/**",
        "com/ourcompany/endpoint/util/ObjectMapperBuilder*",
        "com/ourcompany/framework/**",
        "com/ourcompany/helper/ApiHelper*",
        "com/ourcompany/helper/OffsetDateTimeDeserializer*",
        "com/ourcompany/persistence/entity/**",
        "com/ourcompany/persistence/exception/**",
        "com/ourcompany/service/exception/ServiceException*",
        "com/ourcompany/service/model/**",
        "com/ourcompany/v2/**",
        "com/ourcompany/v3/**"
    ]
}

jacocoTestReport中使用了它们:

jacocoTestReport {
    reports {
        xml {
            enabled true
        }
        html {
            enabled true
        }
    }
    getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec"));
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it,
                exclude: coverageExcludes()
            )
        }))
        for (Object c : classDirectories) {
            System.out.println(c);
        }
    }
}

排除工作:

然后我们尝试在验证任务中使用相同的排除项,将覆盖率失败阈值设置为 0.87,因为这是覆盖率报告中显示的内容:

jacocoTestCoverageVerification {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it,
                exclude: coverageExcludes()
            )
        }))
        for (Object c : classDirectories) {
            System.out.println(c);
        }
    }
    violationRules {
        rule {
            element = 'BUNDLE'
            limit {
                counter = 'INSTRUCTION'
                value = 'COVEREDRATIO'
                minimum = 0.87
            }
        }
    }
}
check.dependsOn jacocoTestCoverageVerification

但这不起作用:

Execution failed for task ':settings:jacocoTestCoverageVerification'.

Rule violated for bundle settings: instructions covered ratio is 0.70, but expected minimum is 0.87

我们比较了两个任务中 classDirectories 的输出,它们完全匹配,排除列表中没有任何内容在 classDirectories 中。这意味着两个任务看到相同的 类.

然而,验证任务得到的覆盖率答案与报告任务不同。

这令人担忧,因为我们不知道哪个任务得到了正确答案。如果是报告任务,我们可以接受验证任务中较低的阈值,但如果我们真的只有 70% 的覆盖率,我们想知道。

正如我们从文档中得知的那样,我们已将验证任务配置为以与报告任务生成的方式完全相同的方式分析覆盖率。

我们做错了什么?

(我们正在使用 Gradle 5.5.1,目前还不能升级,但如果我们能解决这个问题就会升级。)

您在 jacocoTestReport 任务中配置 ExecutionData

getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec"));

但不在 jacocoTestCoverageVerification 任务中。