How to generate jacoco coverage in manual tests?

2.6k views Asked by At

I have a web application running on wildfly 9 using gradle to build it, and I would like to get code coverage of manual tests, so I started using jacoco for doing so. What I have so far is this in my build.gradle file is this for starting java in debug mode:

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
    options.debug = true
    options.compilerArgs = ["-g"]
}

And this for defining jacoco reports

jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

However, it does not generate jacoco folder, I think I am missing some point or something.

2

There are 2 answers

0
Godin On BEST ANSWER

Usage of JaCoCo involves following steps:

  • execution of instrumented code (no matter as manual or automated tests)
  • generation of report

Information that you provide in the question - is about compilation of Java files and generation of report, but nothing about execution of JVM.

There are many ways to execute code with on-the-fly instrumentation depending on how JVM is started (Gradle/Maven/Ant plugins, etc.), but they all boil down to usage of JaCoCo Java Agent while starting JVM:

java -javaagent:jacocoagent.jar ...
0
lance-java On

By default, the jacocoTestReport task is not wired into the DAG for a normal build. To run it you can call the following from command line

./gradlew test jacocoTestReport

If you want it to run every time the tests run (which I don't recommend) then you can wire it onto the DAG in your build.gradle

test.finalizedBy 'jacocoTestReport' // not perfect since it will run when test fails

Or perhaps

check.dependsOn 'jacocoTestReport' // 'build' task calls 'check' which calls 'test'