javagitlabgitlab-cijacoco

GitLab CI&CD test coverage with jacoco


In GitLab 15.0 test coverage feature in settings project will be removed.

GitLab offers to parse test coverage with coverage keyword in .gitlab-ci.yml

All requirements are met:

  1. Defined as single-quoted string.
  2. A regular expression starts and ends with the / character.

Why test-coverage badge is still showing unknown?

test:
  stage: test
  script: gradle check
  coverage: '/Total.*?([0-9]{1,3})%./'
  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: pull
    paths:
      - build
      - .gradle
  artifacts:
    when: always
    reports:
      junit: build/test-results/test/**/TEST-*.xml


Solution

  • 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.]+)%/'