1

I'm trying to get JaCoCo code coverage reports to be generated whenever ./gradlew test is run. I've got the following in my build.gradle file:


apply plugin: "java" // needed by jacoco plugin
apply plugin: "jacoco"


test {
    useJUnitPlatform()
}

jacoco {
    toolVersion = "0.8.5"
    applyTo junitPlatformTest
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
        html.destination file("${buildDir}/jacoco/jacocoHtml")
    }
}

junitPlatformTest {
    jacoco {
        destinationFile =  file("${buildDir}/jacoco/jacocoReport.exec")
    }
}

Whenever I run tests, no xml or html reports are generated. However, JaCoCo does generate a junitPlatformTest.exec file in {buildDir}/jacoco. How can I get it to generate some xml and html reports too?

1

1 Answer 1

1

JaCoCo seems to add a few tasks but doesn't automatically attach them as dependencies to the test tasks.

I have following in build.gradle to make the coverage get generated after the test run:

test.finalizedBy jacocoTestReport

You could use dependsOn rather than finalizedBy but dependsOn won't give you coverage if the tests fail.

Sign up to request clarification or add additional context in comments.

3 Comments

This suggestion does make it so that reports are generated regardless of whether tests pass or fail, but I'm afraid it still generates no html or xml reports.
What happens if you get rid of the html.destination? My build.gradle has more or less the same thing (I don't have jacoco.applyTo set) and the report ends up in build/reports/jacoco/test/html by default (and I recall it being a bit of trial and error to get to that).
Removing the html.destination doesn't seem to change anything. I have jacoco.applyTo set as it's required to set junitPlatformTest { jacoco { destinationFile = file(...) } }, without which no file is generated whatsoever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.