Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage report using gitlab-ci.yml file

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?

like image 572
AshKan Avatar asked Dec 30 '17 09:12

AshKan


People also ask

How does Gitlab calculate code coverage?

How test coverage visualization works. Collecting the coverage information is done via GitLab CI/CD's artifacts reports feature. You can specify one or more coverage reports to collect, including wildcard paths. GitLab then takes the coverage information in all the files and combines it together.

What is proper test coverage?

Test coverage is defined as a technique which determines whether our test cases are actually covering the application code and how much code is exercised when we run those test cases. If there are 10 requirements and 100 tests created and if 90 tests are executed then test coverage is 90%.

What is cobertura?

Simply put, Cobertura is a reporting tool that calculates test coverage for a codebase – the percentage of branches/lines accessed by unit tests in a Java project.

How to check test coverage on GitLab CI?

The tests pass on GitLab CI and the coverage number is shown in the "Jobs" page in its own column, see cypress-example-todomvc-redux/-/jobs The coverage HTML report is available under job's artifacts, where we can browser and see the individual file numbers.

What is GitLab-CI yml?

A .gitlab-ci.yml file might contain: In this example, the build-code-job job in the build stage runs first. It outputs the Ruby version the job is using, then runs rake to build project files. If this job completes successfully, the two test-code-job jobs in the test stage start in parallel and run tests on the files.

How does the pipeline work in GitLab CI/CD?

The full pipeline in the example is composed of three jobs, grouped into two stages, build and test. The pipeline runs every time changes are pushed to any branch in the project. GitLab CI/CD not only executes the jobs but also shows you what’s happening during execution, just as you would see in your terminal:

What happens when I add a yml file to my repository?

When you add a .gitlab-ci.yml file to your repository, GitLab detects it and an application called GitLab Runner runs the scripts defined in the jobs. A .gitlab-ci.yml file might contain:


2 Answers

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.

like image 148
SKBo Avatar answered Oct 02 '22 14:10

SKBo


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.

like image 42
olearycrew Avatar answered Oct 02 '22 14:10

olearycrew