Ralf Wickum
Ralf Wickum

Reputation: 3284

What Gitlab tool used for code coverage reports?

Instead of using JaCoCo, I was told, that there would be an internal Gitlab tool, where I can create test coverage reports?

I couldn't find anything in the Gitlab dashboard menu. The project is a Android App Kotlin project.

Upvotes: 2

Views: 4576

Answers (2)

Erik Roky
Erik Roky

Reputation: 129

Our approach is the following. have to tell Gitlab where your coverage report is, for example we have this setup for a java unit test report "jacoco.xml":

 Unit Test:
      stage: pruebas 
      script:
        - echo "Iniciar Pruebas"
        - mvn $MAVEN_CLI_OPTS test
      artifacts:
        when: always
        reports:
          junit: 
            - target/surefire-reports/*Test.xml
            - target/failsafe-reports/*Test.xml
          cobertura: target/site/jacoco/jacoco.xml

Our summary in Gitlab : enter image description here

Unit Test Detaills: enter image description here

The key is your "jacoco.xml".

Upvotes: 0

Simon Schrottner
Simon Schrottner

Reputation: 4764

the question is what part of Coverage you want to see/have:

  • just a number within the MR - therefore GitLab parses the logoutput of the Jobs
  • coverage visualization within MR - therefore you need to provide a report.

Coverage in Overview

For the coverage in the Overview and just to get a percentage, you need to configure your job with an regex how it can be parsed like

job1:
  # ....
  coverage: '/Code coverage: \d+\.\d+/'

https://docs.gitlab.com/ee/ci/yaml/#coverage

Visualization

We are actually using JaCoCo, but to make the coverage visible and to have the information in Merge Requests you have to convert everything into Cobertura Reports.

There are different approaches to achieve this:

  1. with a gradle-plugin like https://github.com/kageiit/gradle-jacobo-plugin

    the configuration is pretty neat, and if you do have already a gradle build it is easy to integrate

  2. with an own step within the CI Pipeline - see https://docs.gitlab.com/ee/user/project/merge_requests/test_coverage_visualization.html

    test-jdk11:
       stage: test
       image: gradle:6.6.1-jdk11
       script:
         - 'gradle test jacocoTestReport' # jacoco must be configured to create an xml report
       artifacts:
         paths:
           - build/jacoco/jacoco.xml
    
     coverage-jdk11:
       # Must be in a stage later than test-jdk11's stage.
       # The `visualize` stage does not exist by default.
       # Please define it first, or chose an existing stage like `deploy`.
       stage: visualize
       image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.7
       script:
         # convert report from jacoco to cobertura, using relative project path
         - python /opt/cover2cover.py build/jacoco/jacoco.xml $CI_PROJECT_DIR/src/main/java/ > build/cobertura.xml
       needs: ["test-jdk11"]
       artifacts:
         reports:
           cobertura: build/cobertura.xml
    

important to note is that you always will have to tell GitLab CI your path to the artifact for cobertura with

job:
    #...
    artifacts:
      reports:
        cobertura: build/cobertura.xml

Upvotes: 5

Related Questions