简体   繁体   English

如果代码覆盖率低于阈值,则使用jacoco check元素,Gradle jacoco会失败

[英]Gradle jacoco fail if code coverage below a threshold using jacoco check element

I am trying to fail a Gradle Build when the Jacoco code coverage is below a certain percent. 当Jacoco代码覆盖率低于某个百分比时,我试图使Gradle Build失败。

<jacoco:report>

    ...

    <check failonviolation="true">
        <rule element="PACKAGE">
            <limit counter="LINE" value="COVEREDRATIO" minimum="0.80"/>
            <limit counter="CLASS" value="MISSEDCOUNT" maximum="0"/>
        </rule>
    </check>

    ...

</jacoco:report>

How can I add this Ant task to the Gradle jacoco plugin even if it is not directly possible ? 如何将此Ant任务添加到Gradle jacoco插件中,即使它不是直接可行的?

I saw this link- http://forums.gradle.org/gradle/topics/how-to-fail-the-build-on-insufficient-code-coverage 我看到了这个链接 - http://forums.gradle.org/gradle/topics/how-to-fail-the-build-on-insufficient-code-coverage

TIA, TIA,

Vijay 维杰

This has recently improved, as Jacoco coverage verification was added to Gradle 3.4 ( issue #824 )! 随着Jacoco覆盖验证被添加到Gradle 3.4( 问题#824 ),这最近有所改善!

Before Gradle 3.4, you had to resort to hand-crafted workarounds such as this one . 在Gradle 3.4之前,你不得不采用手工制作的解决方法,比如这个

Using Gradle 3.4 or later, you can use the standard Jacoco plugin (example for Gradle 4.0): 使用Gradle 3.4或更高版本,您可以使用标准的Jacoco插件 (Gradle 4.0的示例):

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

In the above example, we check for a minimum line coverage of 50%. 在上面的例子中,我们检查最小线路覆盖率为50%。 More complex violation rules are possible, and it is also possible to combine multiple violation rules. 更复杂的违规规则是可能的,并且还可以组合多个违规规则。 Please refer to the linked documentation. 请参阅链接的文档。

In order to run the thusly configured check, we get the task jacocoTestCoverageVerification . 为了运行如此配置的检查,我们得到任务jacocoTestCoverageVerification A coverage report can be created with jacocoTestReport . 可以使用jacocoTestReport创建覆盖率报告。 These tasks can be run automatically by adding something like this (thx @Thunderforge): 这些任务可以通过添加这样的东西自动运行(thx @Thunderforge):

test.finalizedBy jacocoTestCoverageVerification, jacocoTestReport

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM