Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum code coverage threshold in Jacoco Gradle

Tags:

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

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

like image 938
richersoon Avatar asked Feb 21 '16 19:02

richersoon


People also ask

How code coverage is calculated in JaCoCo?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100. 16) Where is the JaCoCo report generated Gradle?

What is JaCoCo test coverage?

Jacoco is an open source project, which can be used to check production code for test code coverage. It creates reports and integrates well with IDEs like the Eclipse IDE. Integration is also available for other IDEs and continuous integration environments.


2 Answers

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.

like image 176
Joffrey Avatar answered Sep 18 '22 16:09

Joffrey


In an Android application this configuration works:

project: build.gradle

buildscript {     repositories {         google()         jcenter()         maven { url 'https://plugins.gradle.org/m2/' }     }     dependencies {         classpath "com.android.tools.build:gradle:3.1.4"         classpath "org.jacoco:org.jacoco.core:0.8.2"     } } 

app: build.gradle

ext.jacoco_version = '0.8.2' def configDir = "${project.rootDir}/config" def reportDir = "${project.buildDir}/reports" def mainSrc = "$project.projectDir/src/main/java" def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*'] def debugTree = fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: fileFilter) //Jacoco jacocoTestReport apply plugin: 'jacoco' jacoco.toolVersion = jacoco_version task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {     reports {         xml.enabled = false         html.enabled = true     }     sourceDirectories = files([mainSrc])     classDirectories = files([debugTree])     executionData = fileTree(dir: project.buildDir, includes: [             'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'     ]) } task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn: 'jacocoTestReport') {     sourceDirectories = files([mainSrc])     classDirectories = files([debugTree])     executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec")     violationRules {         failOnViolation = true         rule {             limit {                 minimum = 0.7             }         }     } } 

We can execute it in commandline with:

./gradlew jacocoTestCoverageVerification 

I used gradle wrapper 4.4.

like image 22
JuanMoreno Avatar answered Sep 18 '22 16:09

JuanMoreno