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?

Chris Moore
  • 113
  • 1
  • 8
  • If the problem continues despite of applying the workarounds, you may have a look at my answer on [maven jacoco: not generating code coverage report](https://stackoverflow.com/questions/25395255/maven-jacoco-not-generating-code-coverage-report/71661614#71661614). – Murat Yıldız Mar 29 '22 at 12:04

1 Answers1

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.

tschumann
  • 2,776
  • 3
  • 26
  • 42
  • 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. – Chris Moore May 28 '20 at 07:54
  • 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). – tschumann May 28 '20 at 22:19
  • 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. – Chris Moore Jun 11 '20 at 12:53