Minimum code coverage threshold in Jacoco Gradle

richersoon picture richersoon · Feb 21, 2016 · Viewed 31.1k times · Source

How can I set the minimum code coverage in Jacoco Gradle?

I want the build to fail if it is not met.

Answer

Joffrey picture Joffrey · Mar 25, 2017

The feature is now available. You simply need to apply the Gradle JaCoCo plugin and define coverage verification like this:

apply plugin: 'jacoco'

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

// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification

The last line is very important as your build would otherwise not fail unless you explicitly run the jacocoTestCoverageVerification task.

More information on the kind of checks you may add is in the documentation of the plugin.