简体   繁体   中英

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: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 ?

I saw this link- http://forums.gradle.org/gradle/topics/how-to-fail-the-build-on-insufficient-code-coverage

TIA,

Vijay

This has recently improved, as Jacoco coverage verification was added to Gradle 3.4 ( issue #824 )!

Before Gradle 3.4, you had to resort to hand-crafted workarounds such as this one .

Using Gradle 3.4 or later, you can use the standard Jacoco plugin (example for Gradle 4.0):

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

In the above example, we check for a minimum line coverage of 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 . A coverage report can be created with jacocoTestReport . These tasks can be run automatically by adding something like this (thx @Thunderforge):

test.finalizedBy jacocoTestCoverageVerification, jacocoTestReport

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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