learn groovy
learn groovy

Reputation: 527

How to get jacoco code coverage report in gradle project

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.

Upvotes: 7

Views: 61454

Answers (2)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4839

My solution is based on Gradle 7.3.2

So, first of all, apply the Jacoco plugin to your gradle project. Below should be in your build.gradle file. If you already have a plugins section, then just add the entry id 'jacoco' in separate line

plugins {
id 'jacoco'
}

This exposes a task named jacocoTestReport and jacocoTestCoverageVerification.

Yo can optionally configure the task to override default values. For e.g. below code will also generate the xml report for coverage, which is mostly needed by SonarQube to analyse the code coverage.

jacocoTestReport {
    reports {
        xml.required = true
    }
}

In order to generate the coverage report you can execute gradle jacocoTestReport manually

And it will generate reports under build/reports/jacoco/test folder

However, we would want to execute the coverage report post the gradle test tasks.

So, to achieve this, add below entry in gradle file. test.finalizedBy(jacocoTestReport)

Now, even if you do gradle test or gradle clean build (test will be implicitly called), you will still see the jacocoTestReport task being executed and reports getting generated.

Below is the jacoco generated report tree structure.

build/reports/jacoco
└── test
    ├── html
    │   ├── index.html
    │   ├── jacoco-resources
    │   │   ├── branchfc.gif
    │   │   ├── branchnc.gif
    │   │   ├── branchpc.gif
    │   │   ├── bundle.gif
    │   │   ├── class.gif
    │   │   ├── down.gif
    │   │   ├── greenbar.gif
    │   │   ├── group.gif
    │   │   ├── method.gif
    │   │   ├── package.gif
    │   │   ├── prettify.css
    │   │   ├── prettify.js
    │   │   ├── redbar.gif
    │   │   ├── report.css
    │   │   ├── report.gif
    │   │   ├── session.gif
    │   │   ├── sort.gif
    │   │   ├── sort.js
    │   │   ├── source.gif
    │   │   └── up.gif
    │   └── jacoco-sessions.html
    └── jacocoTestReport.xml

Upvotes: 4

VaL
VaL

Reputation: 1167

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.

Upvotes: 12

Related Questions