gradle 当代码覆盖率低于指定值时,生成不会失败

qlfbtfca  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(149)

如果代码覆盖率低于90%,我希望我的构建失败。
为此,我为我的build gradle创建了jacocoTestCoverageVerification任务。

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,但构建没有失败。
为什么它没有失败?

kyvafyod

kyvafyod1#

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.

相关问题