当代码覆盖率低于指定值时,构建不会失败



如果代码覆盖率低于90%,我希望构建失败。

为了做到这一点,我创建了jacocoTestCoverageVerification任务到我的构建gradle。

jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.9
}
}
}
}

然后我在Jenkins管道中调用它

stage('Integration tests coverage report - Windows')
{
when { expression { env.OS == 'BAT' }}
steps {
dir('') {
bat 'gradlew.bat jacocoIntegrationReport'
bat 'gradlew.bat jacocoTestCoverageVerification'
}
}
}

但是我的构建没有失败。我也尝试将最小值设置为1.0,但它也成功了。

我尝试添加check.dependsOn jacocoTestCoverageVerification,但构建没有失败。

为什么它没有失败?

dependencies {
implementation .....
testImplementation(platform('org.junit:junit-bom:5.9.1'))
testImplementation('org.junit.jupiter:junit-jupiter')
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

test {
finalizedBy jacocoTestReport // report is always generated after tests run
}
jacocoTestReport {
dependsOn test // tests are required to run before generating the report
}
jacocoTestReport {
finalizedBy jacocoTestCoverageVerification // report is always generated after tests run
}
jacocoTestReport {
reports {
xml.required = false
csv.required = false
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
}
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.5
}
}
}
}

在命令行上运行

$ ./gradlew :application:jacocoTestReport
> Task :application:test
MessageUtilsTest > testNothing() PASSED
> Task :application:jacocoTestCoverageVerification FAILED
[ant:jacocoReport] Rule violated for bundle application: instructions covered ratio is 0.0, but expected minimum is 0.5
FAILURE: Build failed with an exception.

最新更新