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
Answer from jschnasse on Stack Overflow
Top answer
1 of 2
3

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
2 of 2
2

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

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

Example :

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

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.

Discussions

maven - Code coverage report using gitlab-ci.yml file - Stack Overflow
I need to see code coverage report for a java maven project in Gitlab. According to this, this and some other sources: I added jacoco to the list of plugins in pom.xml. Added pages job to my .gitl... More on stackoverflow.com
🌐 stackoverflow.com
java - GitLab CI&CD test coverage with jacoco - Stack Overflow
By adding the coverage: line to the Ci/CD YAML script now the coverage percentage is back in the GitLab dashboard 2022-08-09T01:59:06.797Z+00:00 ... Save this answer. Show activity on this post. ... #!/bin/bash # Check if the JaCoCo XML file path is provided if [ $# -eq 0 ]; then echo "Please ... More on stackoverflow.com
🌐 stackoverflow.com
FR: Gitlab coverage report
As described in https://medium.com/@kaiwinter/javafx-and-code-coverage-on-gitlab-ci-29c690e03fd6 The percentage of coverage can only be reported to gitlab by print the report to the console. Steps to reproduce JaCoCo version: Latest Oper... More on github.com
🌐 github.com
3
November 20, 2017
Code coverage jacoco
Code coverage badge always showing 13 percent, actual coverage is 62 percent in the MR. Please help me. More on forum.gitlab.com
🌐 forum.gitlab.com
0
0
December 16, 2019
🌐
GitLab
docs.gitlab.com › gitlab docs › use gitlab › use ci/cd to build your application › testing › code coverage
Code coverage | GitLab Docs
Coverage visualization parses a Cobertura or JaCoCo XML report that your test job uploads as a CI/CD artifact.
🌐
GitLab
gitlab.com › gitlab.org › #227345
Support JaCoCo coverage reports for coverage visualization (#227345) · Issues · GitLab.org / GitLab · GitLab
July 8, 2020 - ### Intended users * [Delaney (Development Team Lead)](https://about.gitlab.com/handbook/marketing/product-marketing/roles-personas/#delaney-development-team-lead) * [Sasha (Software Developer)](https://about.gitlab.com/handbook/marketing/product-marketing/roles-personas/#sasha-software-developer) * [Devon (DevOps Engineer)](https://about.gitlab.com/handbook/marketing/product-marketing/roles-personas/#devon-devops-engineer) ### User experience goal Use the nice code coverage visualization feature without having to use outdated tool chains that lack support for modern language versions. ### Proposal It would be helpful, if there was a `artifacts:reports:jacoco` setting, for gathering coverage information from JaCoCo reports.
🌐
DEV Community
dev.to › barg › crack-the-code-seamless-coverage-reports-with-jacoco-and-s3-in-gitlab-ci-4ij2
Seamless Coverage Reports with JaCoCo and S3 in GitLab CI - DEV Community
May 13, 2024 - And finally, elevate the experience ... so we can all be on the same page. Code Coverage - measures the percentage of code that is executed when running automated tests....
🌐
DevOpsSchool.com
devopsschool.com › blog › gitlab-code-coverage-in-java-with-gitlab-complete-guide
Gitlab – Code Coverage in Java with GitLab – Complete Guide
Examine the pom.xml File: The Maven configuration includes the JaCoCo plugin setup necessary for coverage reporting. Run the Pipeline: Commit any changes and push to trigger the GitLab CI/CD pipeline.
Top answer
1 of 9
47

It seems you forgot to add the calls to cat in your .gitlab-ci.yml file.

You should have something like that:

script:
    - mvn $MAVEN_CLI_OPTS test
    - cat target/site/jacoco/index.html

That being said, I don't think this is the best way of doing this, as you need to pollute your output with raw HTML in order to retreive the desired coverage value.

I would recommend using the method described in this pull request instead: https://github.com/jacoco/jacoco/pull/488

  • Keep the jacoco parts in your build.xml
  • Use this awk instruction to print the correct code coverage total:

    awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", 
    instructions, "instructions covered"; print 100*covered/instructions, "% 
    covered" }' target/site/jacoco/jacoco.csv
    
  • Replace the Gitlab CI regexp with what the instruction returns: \d+.\d+ \% covered

Edit:

As of Gitlab 8.17, you can define the regexp directly inside the .gitlab-ci.yml file, as stated in the documentation.

It may seem superfluous, but if this regexp is now part of your repository history, you can change it alongside the other tools used to compute it.

2 of 9
19

GitLab employee here.

If your administrator has GitLab pages set up, you can see the URL that your artifact deployed to by going (on your project) to Settings -> Pages.

There you should see:

Congratulations! Your pages are served under: https://your-namespace.example.com/your-project

Click on that link and you should be good to go! Also we are expanding support for HTML artifacts. This issue and it’s related issues talk about existing and upcoming features that may expand on what you’ve built here.

Find elsewhere
🌐
YouTube
youtube.com › devops hint
Java Code Coverage(JaCoCo) Report using GitLab CI for Java Maven project | JaCoCo with GitLab CI - YouTube
In this Video we are going to cover Java Code Coverage(JaCoCo) Report using GitLab CI for Java Maven project | JaCoCo with GitLab CI#jacocowithgitlab #javaco...
Published: August 29, 2023
Views: 1K
🌐
GitLab
gitlab.com › gitlab kubernetes › code coverage report using gitlab ci for jacoco java maven project
GitLab Kubernetes / code coverage report using gitlab ci for jacoco java maven project · GitLab
code coverage report using gitlab ci for jacoco java maven project · Project information · README · Created on · August 29, 2023 · Loading
🌐
GitLab
gitlab.com › gitlab.org › gitlab foss › repository
doc/ci/testing/test_coverage_visualization/jacoco.md · f85a8fe60b6efd4c6bc272946db0bb304e5f0814 · GitLab.org / GitLab FOSS · GitLab
The JaCoCo coverage reports visualization supports: Instructions (C0 Coverage), ci (covered instructions) in reports. This feature is in beta. If you have any comments, use the feedback issue to provide more details. To configure your pipeline to generate the coverage reports, add a job to your .gitlab...
🌐
Medium
medium.com › slickteam › manage-tests-and-coverage-in-gitlab-ci-4ccf0ddae34a
Manage tests and coverage in Gitlab-CI | by Ronan Barbot | Slickteam | Medium
May 11, 2021 - We use JaCoCo on our Java projects to measure test coverage, and we configured our CI to get the global coverage result in the UI. First, you must get the results from your tests report.
Top answer
1 of 2
7

In order to get ./gradlew test to output a summary of test coverage, I needed to add gradle-jacoco-log to my project.

plugins {
  id 'org.barfuin.gradle.jacocolog' version '2.0.0'
}

test {
  finalizedBy jacocoTestReport
}

jacocoTestReport {
  dependsOn test
}

Which gives me the following console output:

> Task :jacocoLogTestCoverage
Test Coverage:
    - Class Coverage: 100%
    - Method Coverage: 83.6%
    - Branch Coverage: 75%
    - Line Coverage: 85.5%
    - Instruction Coverage: 83.1%
    - Complexity Coverage: 82.5%

I can then choose to report Instruction Coverage to GitLab by adding the following to .gitlab-ci.yml:

test:
  stage: test
  script: gradle check
  coverage: '/    - Instruction Coverage: ([0-9.]+)%/'
2 of 2
0

GitLab Community Edition 15.7.6

parse_jacoco.sh

#!/bin/bash

# Check if the JaCoCo XML file path is provided
if [ $# -eq 0 ]; then
    echo "Please provide the JaCoCo XML report file path"
    exit 1
fi

JACOCO_XML=$1

# Check if the file exists
if [ ! -f "$JACOCO_XML" ]; then
    echo "File not found: $JACOCO_XML"
    exit 1
fi

# Ensure xmllint is installed
if ! command -v xmllint &> /dev/null; then
    echo "xmllint is not installed. Please install libxml2-utils"
    exit 1
fi

# Define a function to calculate coverage percentage
calculate_coverage() {
    local covered=$1
    local missed=$2
    local total=$((covered + missed))
    if [ $total -eq 0 ]; then
        echo "0.0"
    else
        echo "scale=1; $covered * 100 / $total" | bc
    fi
}

echo "Coverage Data:"

# Initialize total instruction coverage counters
total_instruction_covered=0
total_instruction_missed=0

# Get coverage for various types
for type in INSTRUCTION BRANCH LINE COMPLEXITY METHOD CLASS; do
    covered=$(xmllint --xpath "sum(/report/counter[@type='$type']/@covered)" $JACOCO_XML)
    missed=$(xmllint --xpath "sum(/report/counter[@type='$type']/@missed)" $JACOCO_XML)
    total=$((covered + missed))
    coverage=$(calculate_coverage $covered $missed)
    echo "${type} Coverage: ${covered} covered, ${missed} missed, ${total} total (${coverage}%)"

    # Accumulate INSTRUCTION coverage as the total coverage
    if [ "$type" = "INSTRUCTION" ]; then
        total_instruction_covered=$covered
        total_instruction_missed=$missed
    fi
done

# Calculate and output total coverage (echo here for GitLab usage)
total_coverage=$(calculate_coverage $total_instruction_covered $total_instruction_missed)
echo "Total Coverage: ${total_coverage}%"

gitlab config

stages:
  - test
  - visualize

test-job:
  stage: test
  image: maven:3.3-jdk-8
  script:
    - mvn verify
  artifacts:
    when: always
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml
        - target/failsafe-reports/TEST-*.xml
    paths:
      - target/site/jacoco/jacoco.xml
  tags:
    - docker-runner

coverage-job:
  stage: visualize
  image: alpine:latest
  before_script:
    - apk add --no-cache libxml2-utils
  script:
    - ./parse_jacoco.sh target/site/jacoco/jacoco.xml
  coverage: '/Total Coverage: (\d+\.\d+)%/'
  needs: ["test-job"]
  tags:
    - docker-runner

🌐
GitLab
gitlab.lexilogos.com › help › help
Jacoco · Code coverage · Testing · Ci · Help · GitLab
Introduced in GitLab 17.3 with a flag named jacoco_coverage_reports.
🌐
GitLab
docs.gitlab.com › gitlab docs › use gitlab › use ci/cd to build your application › testing › code coverage › coverage visualization › cobertura
Cobertura coverage visualization | GitLab Docs
test: script: - npm install - npx nyc --reporter cobertura mocha artifacts: reports: coverage_report: coverage_format: cobertura path: coverage/cobertura-coverage.xml · GitLab 17.6 and later supports JaCoCo format natively.
🌐
Reddit
reddit.com › r/gitlab › java code coverage(jacoco) report using gitlab ci for java maven project...
r/gitlab on Reddit: Java Code Coverage(JaCoCo) Report using GitLab CI for Java Maven project...
August 29, 2023 - GitLab is the DevSecOps platform. Software. Faster. Members · Online • · fosstechnix · Share · Share · Share · Be the first to comment · Nobody's responded to this post yet. Add your thoughts and get the conversation going. Instant Code Coverage · r/programmingmemes • ·
🌐
GitHub
github.com › diffblue › gitlab › blob › master › doc › ci › testing › test_coverage_visualization.md
gitlab/doc/ci/testing/test_coverage_visualization.md at master · diffblue/gitlab
The following .gitlab-ci.yml example for Java or Kotlin uses Maven to build the project and JaCoCo coverage-tooling to generate the coverage artifact. You can check the Docker image configuration and scripts if you want to build your own image. GitLab expects the artifact in the Cobertura format, so you have to execute a few scripts before uploading it. The test-jdk11 job tests the code and generates an XML artifact.
Author: diffblue
🌐
OpenMSCG
software.rcc.uchicago.edu › help
Test coverage visualization · Merge requests · Project · User · Help · GitLab
The following gitlab-ci.yml example for Java or Kotlin uses Maven to build the project and Jacoco coverage-tooling to generate the coverage artifact. You can check the Docker image configuration and scripts if you want to build your own image. GitLab expects the artifact in the Cobertura format, so you have to execute a few scripts before uploading it. The test-jdk11 job tests the code and generates an XML artifact.
🌐
GitHub
github.com › jacoco › jacoco › issues › 623
FR: Gitlab coverage report · Issue #623 · jacoco/jacoco
November 20, 2017 - As described in https://medium.com/@kaiwinter/javafx-and-code-coverage-on-gitlab-ci-29c690e03fd6 The percentage of coverage can only be reported to gitlab by print the report to the console. JaCoCo version: Latest Operating system: All Tool integration: Maven, Gitlab, Github
Author: jacoco
🌐
Jfsanchez
notes.jfsanchez.net › 2022 › 07 › 15 › gitlab-coverage-from-jacoco-reports
GitLab coverage from JaCoCo reports – Notes
July 15, 2022 - stages: - test test:unit: stage: test image: openjdk:17-alpine script: | ./gradlew check # Print test coverage to console echo "coverage: $(cat build/reports/jacoco/test/html/index.html | grep -Eo 'Total[^%]*%' | grep -Eo '([0-9]{1,3})%')" coverage: "/coverage: ([0-9]{1,3}%)/" artifacts: reports: junit: - build/test-results/test/TEST-*.xml coverage_report: coverage_format: cobertura path: build/reports/cobertura/cobertura.xml · With this pipeline configuration, we should be able to collect the application test coverage and visualize this information inside the file diff view of our Merge Requests (MRs).
🌐
GitLab
forum.gitlab.com › gitlab ci/cd
Code coverage jacoco - GitLab CI/CD - GitLab Forum
December 16, 2019 - Code coverage badge always showing 13 percent, actual coverage is 62 percent in the MR. Please help me.