Gradle jacoco 的构建控制台输出说它比实际情况低 %

Gradle build console output for jacoco says it's lower % than it really is

我正在尝试使用 jacoco 进行 gradle 构建,但它失败了,因为覆盖率低于 "expected"。但是当我打开生成的HTML文件时,它实际上高于我设置的最小值。

我试过从分析中排除更多文件,但没有帮助。


jacoco {
    toolVersion = "0.8.4"
//    applyTo test
}

jacocoTestReport.dependsOn(test)

// this just ensures it runs with for build, but not for non-builds (like tests)
build {
    finalizedBy jacocoTestReport

    // this will do checks on the violations + fail build if there's too little coverage
    finalizedBy jacocoTestCoverageVerification
}

jacocoTestReport {
    reports {
        csv.enabled false
        xml.enabled false
        html.enabled true

        // setting the output location for html stuff
        html.destination file("${buildDir}/reports/jacoco")
    }

    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: [...files...])
        }))
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                // % in decimal form for when build should fail
                minimum = 0.75
            }
        }
    }
}

我在构建中得到的信息是:说明覆盖率为 0.74,但预期最小值为 0.75

但是正如您在 https://i.imgur.com/MO2GMlK.png 中看到的那样,它是 80%。为什么会这样?

class 排除项可能未应用于 jacocoTestCoverageVerification 任务。

您是否也尝试过在 jacocoTestCoverageVerification 任务中应用排除?

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                // % in decimal form for when build should fail
                minimum = 0.75
            }
        }
    }

    afterEvaluate {
            classDirectories.setFrom(files(classDirectories.files.collect {
                fileTree(dir: it, exclude: [...files...])
            }))
        }
    }
}

类似于: