Back to Gitlabhq

Coverage reporting

doc/ci/testing/code_coverage/coverage_reporting.md

19.1.07.3 KB
Original Source

{{< details >}}

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

{{< /details >}}

Use the coverage keyword to extract a coverage percentage from your test job's log output and display it in merge requests and analytics.

This keyword displays a coverage percentage only. It does not produce line-by-line annotations in the MR diff. To display line annotations, configure artifacts:reports:coverage_report separately.

Configure coverage reporting

To configure coverage reporting:

  1. Add the coverage keyword to your job with a regular expression that matches your test tool's output:

    yaml
    test:
      script:
        - pytest --cov
      coverage: '/TOTAL.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
    
  2. To aggregate coverage from multiple jobs, add the coverage keyword to each job.

Coverage regex patterns

The following regex patterns match output from common test coverage tools. Test these carefully, as tool output formats can change over time.

{{< tabs >}}

{{< tab title="Python and Ruby" >}}

ToolLanguageCommandRegex pattern
pytest-covPythonpytest --cov/TOTAL.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/
Simplecov-htmlRubyrspec spec/Line\sCoverage:\s\d+\.\d+%/

{{< /tab >}}

{{< tab title="C/C++ and Rust" >}}

ToolLanguageCommandRegex pattern
gcovrC/C++gcovr/^TOTAL.*\s+(\d+\%)$/
tarpaulinRustcargo tarpaulin/^\d+.\d+% coverage/

{{< /tab >}}

{{< tab title="Java and JVM" >}}

ToolLanguageCommandRegex pattern
JaCoCoJava/Kotlin./gradlew test jacocoTestReport/Total.*?([0-9]{1,3})%/
ScoverageScalasbt coverage test coverageReport/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/

{{< /tab >}}

{{< tab title="Node.js" >}}

ToolCommandRegex pattern
taptap --coverage-report=text-summary/^Statements\s*:\s*([^%]+)/
nycnyc npm test/All files[^|]*|[^|]*\s+([\d\.]+)/
jestjest --ci --coverage/All files[^|]*|[^|]*\s+([\d\.]+)/
node:testnode --experimental-test-coverage --test/all files[^|]*|[^|]*\s+([\d\.]+)/

{{< /tab >}}

{{< tab title="PHP" >}}

ToolCommandRegex pattern
pestpest --coverage --colors=never/Statement coverage[A-Za-z\.*]\s*:\s*([^%]+)/
phpunitphpunit --coverage-text --colors=never/^\s*Lines:\s*\d+.\d+\%/

{{< /tab >}}

{{< tab title="Go" >}}

ToolCommandRegex pattern
go test (single)go test -cover/coverage: \d+.\d+% of statements/
go test (project)go test -coverprofile=cover.profile && go tool cover -func cover.profile/total:\s+\(statements\)\s+\d+.\d+%/

{{< /tab >}}

{{< tab title=".NET and PowerShell" >}}

ToolLanguageCommandRegex pattern
OpenCover.NETNone/(Visited Points).*\((.*)\)/
dotnet test.NETdotnet test/Total\s*|*\s(\d+(?:\.\d+)?)/
PesterPowerShellNone/Covered (\d{1,3}(\.|,)?\d{0,2}%)/

{{< /tab >}}

{{< tab title="Elixir" >}}

ToolCommandRegex pattern
excoverallsNone/\[TOTAL\]\s+(\d+\.\d+)%/
mixmix test --cover/\d+.\d+\%\s+|\s+Total/

{{< /tab >}}

{{< /tabs >}}

Add a coverage check approval rule

{{< details >}}

  • Tier: Premium, Ultimate

{{< /details >}}

You can require specific users or a group to approve merge requests that reduce the project's test coverage.

Prerequisites:

  • Configure coverage reporting.

To add a Coverage-Check approval rule:

  1. In the top bar, select Search or go to and find your project.
  2. In the left sidebar, select Settings > Merge requests.
  3. Under Merge request approvals, do one of the following:
    • Next to the Coverage-Check approval rule, select Enable.
    • For manual setup, select Add approval rule, then enter Coverage-Check as the Rule name.
  4. Select a Target branch.
  5. Set the number of Required number of approvals.
  6. Select the Users or Groups to provide approval.
  7. Select Save changes.

[!note] The Coverage-Check approval rule requires approval when the merge base pipeline contains no coverage data, even if the merge request improves overall coverage.

View coverage history

You can track coverage trends for your project or group over time.

For a project

  1. In the top bar, select Search or go to and find your project.
  2. In the left sidebar, select Analyze > Repository analytics.
  3. From the dropdown list, select the job you want to view historical data for.
  4. Optional. To download the data, select Download raw data (.csv).

For a group

{{< details >}}

  • Tier: Premium, Ultimate

{{< /details >}}

  1. In the top bar, select Search or go to and find your group.
  2. In the left sidebar, select Analyze > Repository analytics.
  3. Optional. To download the data, select Download historic test coverage data (.csv).

Display coverage badges

To add a coverage badge to your project, see test coverage report badges.

Troubleshooting

When working with coverage reporting, you might encounter the following issues.

Coverage percentage does not appear in the MR widget

The coverage keyword extracts a percentage from your job's log output using a regular expression. If the percentage does not appear:

  • Verify your regex matches your tool's actual output. Copy a line from the job log and test it against your regex.

  • Some tools output ANSI color codes that break regex matching. If your tool does not support disabling color output, strip the codes before parsing:

    shell
    lein cloverage | perl -pe 's/\e\[?.*?[\@-~]//g'
    
  • Check that the job completed successfully. Coverage is only extracted from successful jobs.

  • Coverage output from child pipelines is not recorded. For details, see issue 280818.

[!note] The coverage keyword only shows a percentage in the MR widget. For line-by-line annotations in the diff, configure artifacts:reports:coverage_report separately.