Code coverage

  • Tier : Free, Premium, Ultimate
  • Offering : GitLab.com, GitLab Self-Managed, GitLab Dedicated

Configure code coverage to track and visualize how much of your source code is covered by tests. You can:

  • Track overall coverage metrics and trends using the coverage keyword.
  • Visualize line-by-line coverage using the artifacts:reports:coverage_report keyword.

Configure coverage reporting

Use the coverage keyword to monitor your test coverage and enforce coverage requirements in merge requests.

With coverage reporting, you can:

  • Display the overall coverage percentage in merge requests.
  • Aggregate coverage from multiple test jobs.
  • Add coverage check approval rules.
  • Track coverage trends over time.

To configure coverage reporting:

  1. Add the coverage keyword to your pipeline configuration:

    test-unit:
      script:
        - coverage run unit/
      coverage: '/TOTAL.+ ([0-9]{1,3}%)/'
    test-integration:
      script:
        - coverage run integration/
      coverage: '/TOTAL.+ ([0-9]{1,3}%)/'
  2. Configure the regular expression (regex) to match your test output format. See coverage regex patterns for common patterns.

  3. To aggregate coverage from multiple jobs, add the coverage keyword to each job you want to include.

  4. Optional. Add a coverage check approval rule .

Coverage regex patterns

The following sample regex patterns were designed to parse coverage output from common test coverage tools.

Test the regex patterns carefully. Tool output formats can change over time, and these patterns might no longer work as expected.