PatPanda
PatPanda

Reputation: 5070

Gitlab 17.4 JaCoCo coverage report

We would like to see the visual of the Jacoco code coverage report in gitlab.

This question is not about cobertura.

It seems the feature of displaying Jacoco coverage is now ready, starting gitlab 17.4. https://gitlab.com/gitlab-org/gitlab/-/issues/227345

I verified our gitlab is 17.4.

We went to follow this guide: https://docs.gitlab.com/ee/ci/testing/code_coverage/jacoco.html https://docs.gitlab.com/ee/ci/testing/code_coverage/index.html?tab=Java+and+JVM

And wrote the following block in the gitlab ci yml file:

coverage:
  script:
    - mvn clean install surefire-report:report
  image: maven:3.9.9-sapmachine-23
  stage: coverage
  artifacts:
    paths: [ target/ ]
    reports:
      junit: target/surefire-reports/TEST-*.xml
      coverage_report:
        coverage_format: jacoco
        path: target/site/jacoco/jacoco.xml

The job is successful.

We can even see the file target/site/jacoco/jacoco.xml

However, I am not seeing the coverage report anywhere.

May I ask if we missed some step?

Or we did everything correctly, in which case, where can I see the Jacoco coverage report?

Upvotes: 2

Views: 108

Answers (2)

jschnasse
jschnasse

Reputation: 9588

You must add coverage keyword to your .gitlab-ci.yml See: https://docs.gitlab.com/ee/ci/testing/code_coverage/index.html

You can calculate coverage from the output

...
script:
    - awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", instructions, " instructions covered"; print 100*covered/instructions, "% covered" }' target/site/jacoco/jacoco.csv
  coverage: '/\d+\.\d+.%.covered/'
...

Example

clean-test-jacoco:
  image: maven:latest
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS clean verify
    - awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", instructions, " instructions covered"; print 100*covered/instructions, "% covered" }' target/site/jacoco/jacoco.csv
  coverage: '/\d+\.\d+.%.covered/'
  artifacts:
    paths:
      - target/site/jacoco/jacoco.xml
    reports:  
      coverage_report:
        coverage_format: jacoco
        path: target/site/jacoco/jacoco.xml

Also you can create a badge

![coverage](https://gitlab.example.com/<namespace>/<project>/badges/<branch>/coverage.svg?job=coverage)

Upvotes: 1

fmdaboville
fmdaboville

Reputation: 1831

You should see them in the Tests tab on the Pipelines :

https://gitlab.com/{group}/{project}/-/pipelines/{pipeline_id}/test_report

Example :

Test tab on pipeline

You also have access to the report on the Merge Request if you have one open :

mr coverage

To see the coverage you need to configure the coverage keyword in the job :

coverage:
  script:
    - mvn clean install surefire-report:report
  image: maven:3.9.9-sapmachine-23
  stage: coverage
  coverage: /Total.*?([0-9]{1,3})%/
  artifacts:
    [...]

This will tell to Gitlab the regex to use to get the coverage based on your coverage tool, here Jacoco. Based on the documentation :

After a pipeline runs successfully, you can view code coverage results in:

  • Merge request widget: See the coverage percentage and changes compared to the target branch.

  • Merge request widget showing code coverage percentage

  • Merge request diff: Review which lines are covered by tests. Available with Cobertura and JaCoCo reports. Pipeline jobs: Monitor coverage results for individual jobs.

Upvotes: 2

Related Questions