How to get jacoco code coverage report in gradle project

13,005

Option 1

Run gradle build jacocoTestReport to generate JaCoCo code coverage report.

Option 2.1

Make tasks dependent in your Gradle scripts:

build.dependsOn jacocoTestReport

and then just run gradle build. JaCoCo report will be generated at each build task execution.

Option 2.2 (proposed by Filip Malczak)

Add to your Gradle scripts:

test.doLast jacocoTestReport.&execute

It works in similar fashion as the previous option, but generates report after every execution of test task. It can be useful if you tend to work by running test instead of build.

Share:
13,005
learn groovy
Author by

learn groovy

Updated on June 04, 2022

Comments

  • learn groovy
    learn groovy almost 2 years

    I'm new to Groovy gradle world, I've written small groovy project with all test cases. I'm trying to generate code coverage report using jacoco plugin. But it generates only test report not code coverage report. Please find my jacoco gradle file config. I'm using gradle 4.5 version and Groovy Version: 2.5.0-rc-2 JVM: 1.8.0_171 Vendor: Oracle Corporation OS: Windows 10

    What I'm missing here in order to generate jacoco code coverage report in html format.

    Appreciated your help in advance!

    My jacoco.gradle

    apply plugin: "jacoco"
    
    jacoco {
      toolVersion = '0.8.1'
    }
    
    jacocoTestReport {
      group = "reporting"
      description = "Generate Jacoco coverage reports after running tests."
      reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
      }
      sourceDirectories = fileTree(dir: 'src/main/myproject')
    }
    

    I'm running gradle clean build command to generate build folder under my project repository.

  • Filip Malczak
    Filip Malczak almost 6 years
    Alternatively to option #2 - add test.doLast jacocoTestReport.&execute - works in similiar fashion, but generates report after every execution of test. Can be useful if you tend to work by running test instead of build and I personally prefer it. @VaL - feel free to edit your answer and add it, this is still your answer, I'm just suggesting.
  • 62mkv
    62mkv about 4 years
    this last option does not work for me with "> No signature of method: org.gradle.testing.jacoco.tasks.JacocoReport_Decorated.execu‌​te() is applicable for argument types: () values: [] "