How to: GitLab test coverage with JaCoCo and Gradle

Cover Image for How to: GitLab test coverage with JaCoCo and Gradle
Adam Kobor
Adam Kobor
| 2 min read

It's pretty cool if you have informative badges in your README file about your project's "health", and fortunately GitLab has built-in support for two types of CI/CD badges (pipeline status and test coverage). However, it's far from clear how you can make the latter work with JaCoCo and Gradle.

Gradle and JaCoCo

JaCoCo can generate coverage reports in multiple formats, we will use CSV in this case. The generation of CSV reports is disabled by default, so first you have to enable it in your build.gradle (or build.gradle.kts if you are using Kotlin DSL):

jacocoTestReport {
    reports {
        csv.enabled true
        csv.destination file("${buildDir}/jacocoCsv")
    // ... Additional configuration for JaCoCo 

test {

Please note that we've also specified where this CSV should be generated to, and told Gradle that it should finalize the test task by running JaCoCo.


In your .gitlab-ci.yml you have to parse and print the summary of your coverage report, because GitLab can get this information from nowhere, but your job logs. The only magic here is the awk command, which makes a summary from the CSV and prints it to the log:

# ...
# ...
  - test
# ...
  stage: test
    # Any task that runs your tests
    - ./gradlew check 
    - awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", instructions, " instructions covered"; print 100*covered/instructions, "% covered" }' $JACOCO_CSV_LOCATION

Pay attention to the JACOCO_CSV_LOCATION variable's value, and how it relates to the destination we set in build.gradle.

GitLab settings

The last step is to provide a regular expression that has a capturing group for our test coverage. You can set this is at Settings -> CI/CD -> General pipelines -> Test coverage parsing .

Test coverage parsing

The expression is the following: ([0-9]{1,3}.[0-9]*).%.covered

Basically that's all. If you set up everything correctly, then right after a new, successful build you should see something like this on this page at the "Coverage report" section:

Test coverage badga