Android + JaCoCo: jacocoTestCoverageVerification never fails

810 Views Asked by At

I am trying to configure a new Android project to use JaCoCo for test coverage and fail when coverage is below a certain threshold. I have used this documentation, as well as looking at many SO posts. I expect the task to fail, since I don't have test coverage, but it succeeds without any warnings.

My :app build.gradle file includes the following:

plugins {
    ...
    id 'jacoco'
}

jacoco {
    toolVersion = "0.8.7"
    reportsDirectory = layout.buildDirectory.dir('app/build/reports')
}

task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn: ['jacocoTestReport']) {
    onlyIf = { true }
    violationRules {
        failOnViolation = true
        rule {
            limit {
                minimum = 1.0
            }
        }
    }
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
    finalizedBy jacocoTestCoverageVerification

    onlyIf = {true}
    reports {
        xml.enabled  true
        html.enabled  true
        html.outputLocation = layout.buildDirectory.dir('app/build/reports')
    }


}

tasks.withType(Test) {
    finalizedBy jacocoTestReport

    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

android {
    ...
    jacoco{
        version  = "0.8.7"
    }
}

When I try to run tests, using any of the following:

./gradlew test
./gradlew jacocoTestReport
./gradlew jacocoTestCoverageVerification

The results are always the same:

...

> Task :app:connectedDebugAndroidTest
Starting 2 tests on Nexus 5X - 8.1.0

> Task :app:createDebugAndroidTestCoverageReport
> Task :app:createDebugCoverageReport
> Task :app:jacocoTestReport
> Task :app:jacocoTestCoverageVerification

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 31s
93 actionable tasks: 93 executed

Build Analyzer results available
1:10:48 PM: Task execution finished 'test'.

I expect the task to fail, since I have no test coverage. I have added a utility class and method, and partially implemented a test so that the JaCoCo report shows 60% coverage for one class, but this has not changed anything.

0

There are 0 best solutions below