Search code examples
gradlejunitjacocojacoco-plugin

How to configure Jacoco plugin to read from custom gradle test task output?


Some of my tests need a special gradle task to run. The special task sets an environment variable and runs all JUnit tests that are conditioned by that variable.

When I run the custom task and then the JacocoTestReport, the report Jacoco HTML is not updated.

Here the code for the custom test task: build.gradle

plugins {
    id 'java'
    id 'jacoco'
}
tasks.register('testAll', Test) {
    environment "FILTER", "ALL"           
    useJUnitPlatform()
}

...


Solution

  • The task must save the jacoco intermediate file at the same location that the test task.

    To do so, the destination file of jacoco must be changed to point jacoco/test/exec

    Here the complete code:

    //build.gradle
    
    plugins {
        id 'java'
        id 'jacoco'
    }
    tasks.register('testAll', Test) {
        environment "FILTER", "ALL"
        jacoco {
            destinationFile = layout.buildDirectory.file('jacoco/test.exec').get().asFile
        }
        useJUnitPlatform()
    }
    

    To get HTML the report, run first the testAll task. The test.exec file will be generated at the proper location.

    Then the JacocoTestReport task and you will have the HTML report refreshed.