0

I want my build to fail if the code coverage is below 90%.

In order to do that, I created the jacocoTestCoverageVerification task to my build gradle.

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

And then I called it in my Jenkins pipeline

stage('Integration tests coverage report - Windows')
                        {
                            when { expression { env.OS == 'BAT' }}
                            steps {
                                dir('') {
                                     bat 'gradlew.bat jacocoIntegrationReport'
                                     bat 'gradlew.bat jacocoTestCoverageVerification'
                                }
                            }
                        }

But my build is not failing. I also tried to set the minimum to 1.0, but it was also successful.

I tried to add check.dependsOn jacocoTestCoverageVerification, but the build didn't fail.

Why is it not failing?

Ken White
  • 123,280
  • 14
  • 225
  • 444

1 Answers1

0
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
            }
        }
    }
}

run on command line

$ ./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.
PrasadU
  • 2,154
  • 1
  • 9
  • 10
  • I already have a stage previously to the one for the coverage report for running the integration tests. Do I still need test { finalizedBy jacocoTestReport // report is always generated after tests run } jacocoTestReport { dependsOn test // tests are required to run before generating the report } ? – Fábio Pires Dec 18 '22 at 12:28
  • it didn't work. It stills doesn't fail – Fábio Pires Dec 19 '22 at 21:34