Search code examples
gradlejacoco

Gradle skip JaCoCo during test


I have an issue in which test task is failing intermittently (I doubt it is, because Jacoco is triggering [generating reports] during test).

Is there a way to disable running of jacoco during test?

I can create a new task solely for generating coverage reports (I would like to know how that can be done too). There are many cases in which I don't really need a test coverage report while running test task.


Solution

  • Edit


    After reading the second answer and testing it myself I greatly suggest and appreciate the below blog post as it's true the instrumentation still happens even after disabling or removing the tasks.

    https://poetengineer.postach.io/post/how-to-conditionally-enable-disable-jacoco-in-gradle-build


    If it does runs on Jenkins, error 137 might be out of memory issue.

    If it runs on Jenkins please try to extend the memory and check this link.

    I'm getting OutOfMemoryError

    Jacoco tasks:

    jacocoTestReport    -   JacocoReport    Generates code coverage report for the test task.
    jacocoTestCoverageVerification  -   JacocoCoverageVerification  Verifies code coverage metrics based on specified rules for the test task.
    

    To find out which tasks have which dependencies you can do

    gradle tasks --all
    

    To exclude the task with cmd you can

    gradle test -x taskToExclude
    

    Programmatically you can use a task graph to exclude it

    gradle.taskGraph.useFilter { task -> yourstuff}
    

    or simply remove it from test task dependencies

    test.dependsOn.remove("jacocoTestReport")
    test.dependsOn.remove("jacocoTestCoverageVerification")
    

    Additional resource worth checking: https://docs.gradle.org/current/userguide/jacoco_plugin.html