Code coverage report using gitlab-ci.yml file
Asked Answered
Y

9

42

I need to see code coverage report for a java maven project in Gitlab. According to this, this and some other sources:

  1. I added jacoco to the list of plugins in pom.xml.
  2. Added pages job to my .gitlab-ci.yml file.
  3. Added Total.*?([0-9]{1,3})% to code coverage parsing in project setting.

but there isn't any coverage report or at least I can't see it. There is no coverage percentage or coverage report page.

Content of .gitlab-ci.yml file:

image: maven:latest

variables:
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"

cache:
  paths:
    - .m2/repository/

build:
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS compile

test:
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS test
  artifacts:
    paths:
      - target/site/jacoco/
pages:
  stage: deploy
  dependencies:
    - test
  script:
   - mkdir public
   - mv target/site/jacoco/index.html public
  artifacts:
    paths:
      - public

deploy:
  stage: deploy
  script:
    - mvn $MAVEN_CLI_OPTS verify
  only:
    - master

jacoco plugin in pom.xml:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

My Project is a private project on gitlab.com.

Pipeline and its all 4 jobs passed successfully.

How can I see the coverage reports?

Yetty answered 30/12, 2017 at 9:52 Comment(1)
Recently I faced the same issue, I documented my solution here notes.jfsanchez.net/post/…Bulimia
L
46

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.

Labrador answered 13/2, 2018 at 11:39 Comment(1)
if you use multi module project you can do awk -F"," '{​ instructions += $4 + $5; covered += $5 }​ END {​ print 100*covered/instructions, "% tu covered" }​' `find . -name "jacoco.csv"`Orangery
A
18

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.

Avuncular answered 1/1, 2018 at 15:34 Comment(1)
Thank you. I found pages and the link to coverage report html file. But this file contains only simple percentage of code coverage. Do you know how can I see coverage on code? I mean seeing covered code as green lines, uncovered codes as red lines or something like that?Yetty
F
16

In addition to what @SKBo said I would like to add a small tweak.

Having

cat target/site/jacoco/index.html

will pollute you build output making hard to read what is important.

I would suggest it to:

cat your/path/to/jacoco/report/index.html | grep -o '<tfoot>.*</tfoot>'

That will reduce the noise in a great manner

Fen answered 8/3, 2019 at 11:59 Comment(1)
The link is now jacoco/report/html/index.html So, the final command would be cat your/path/to/jacoco/report/html/index.html | grep -o '<tfoot>.*</tfoot>'Parve
H
9

In order to display the basic Total Coverage Percentage all you need is:

test:
  stage: test
  image: maven:3.6.3-jdk-11
    - mvn $MAVEN_CLI_OPTS test
    - cat target/site/jacoco/index.html | grep -o 'Total[^%]*%'
  coverage: '/Total.*?([0-9]{1,3})%/'
  artifacts:
    paths:
      - target/site/jacoco/jacoco.xml
    expire_in: 1 mos
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

and if you want to enable the Code Coverage Visualization feature:

visualize_test_coverage:
  stage: visualize_test_coverage
  image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.7
  script:
    - 'python /opt/cover2cover.py target/site/jacoco/jacoco.xml src/main/java > target/site/cobertura.xml'
    - 'python /opt/source2filename.py target/site/cobertura.xml'
  needs: [ "test" ]
  dependencies:
    - test
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: target/site/cobertura.xml
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

for detailed information check the official Gitlab Docs here

Huxham answered 31/3, 2021 at 16:56 Comment(1)
Hi @magiccrafter, my target folder is not getting jacoco folder automatically what should i do?Titlark
H
8

I use this command in .gitlab-ci.yml

cat target/site/jacoco-merge/index.html | grep -o 'Total[^%]*%' | sed 's/<.*>/ /; s/Total/Jacoco Coverage Total:/'

This prints a nice string without mess and html tags:

Jacoco Coverage Total: 39%

and then you can use the regex mentioned in gitlabs documentation:

Total.*?([0-9]{1,3})%

or you can use:

Jacoco Coverage Total: ([0-9]{1,3})%
Hardigg answered 9/3, 2021 at 13:37 Comment(0)
O
6

I am using this code:

image: maven:latest

sonarqube-check:
  script:
    - mvn verify sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_TOKEN -Dsonar.qualitygate.wait=true
    - cat target/site/jacoco/index.html | grep -o '.*'
  allow_failure: false
  coverage: "/Total.*?([0-9]{1,3})%/"
Okun answered 26/2, 2020 at 23:58 Comment(0)
I
1

Rather than trying to parse the HTML output, this short awk script extracts the percentage from the jococo.csv file.

#!/bin/sh

# This awk script calculates a code coverage %
# usage: pass the the jacoco.csv file as first argument

awk -F "," '
    {
      instructions += $4 + $5; covered += $5
    }
    END {
      print covered, "/", instructions, "instructions covered";
      printf "%.2f%% covered\n", covered / instructions * 100
    }' "$1"

Prints:

coverage.sh target/site/jacoco/jacoco.csv 
369992 / 469172 instructions covered
78.86% covered

You should adjust the printf format to match your Gitlab coverage regex

Isopod answered 20/5, 2021 at 20:0 Comment(1)
Note that it's also possible to use a #!/usr/bin/awk shebang, but it's less portable due to some compatibility issues with different distros and versions of gawk.Isopod
A
1

full CI example, based on previous answers, working for both unit & integration tests

test:
  stage: test
  image: maven:3.8-openjdk-17-slim
    - mvn $MAVEN_CLI_OPTS verify
      # jacoco code-coverage reporting
    - if [ -f target/site/jacoco/index.html ]; then awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print 100*covered/instructions, "% covered" }' target/site/jacoco/jacoco.csv; fi
    - if [ -f target/site/jacoco-it/index.html ]; then awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print 100*covered/instructions, "% covered" }' target/site/jacoco-it/jacoco.csv; fi
  coverage: '/([0-9.]*) % covered/'
Abolition answered 5/2, 2022 at 5:4 Comment(0)
H
0

add a new job , exec script

awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print int(100*covered/instructions), "% covered" }' target/site/jacoco/jacoco.csv

GET THIS

74 % covered

Hadleigh answered 15/11, 2021 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.