doc/ci/testing/code_coverage/coverage_reporting.md
{{< details >}}
{{< /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.
To configure coverage reporting:
Add the coverage keyword to your job with a regular expression that matches
your test tool's output:
test:
script:
- pytest --cov
coverage: '/TOTAL.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
To aggregate coverage from multiple jobs, add the coverage keyword to each job.
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" >}}
| Tool | Language | Command | Regex pattern |
|---|---|---|---|
| pytest-cov | Python | pytest --cov | /TOTAL.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/ |
| Simplecov-html | Ruby | rspec spec | /Line\sCoverage:\s\d+\.\d+%/ |
{{< /tab >}}
{{< tab title="C/C++ and Rust" >}}
| Tool | Language | Command | Regex pattern |
|---|---|---|---|
| gcovr | C/C++ | gcovr | /^TOTAL.*\s+(\d+\%)$/ |
| tarpaulin | Rust | cargo tarpaulin | /^\d+.\d+% coverage/ |
{{< /tab >}}
{{< tab title="Java and JVM" >}}
| Tool | Language | Command | Regex pattern |
|---|---|---|---|
| JaCoCo | Java/Kotlin | ./gradlew test jacocoTestReport | /Total.*?([0-9]{1,3})%/ |
| Scoverage | Scala | sbt coverage test coverageReport | /(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/ |
{{< /tab >}}
{{< tab title="Node.js" >}}
| Tool | Command | Regex pattern |
|---|---|---|
| tap | tap --coverage-report=text-summary | /^Statements\s*:\s*([^%]+)/ |
| nyc | nyc npm test | /All files[^|]*|[^|]*\s+([\d\.]+)/ |
| jest | jest --ci --coverage | /All files[^|]*|[^|]*\s+([\d\.]+)/ |
| node:test | node --experimental-test-coverage --test | /all files[^|]*|[^|]*\s+([\d\.]+)/ |
{{< /tab >}}
{{< tab title="PHP" >}}
| Tool | Command | Regex pattern |
|---|---|---|
| pest | pest --coverage --colors=never | /Statement coverage[A-Za-z\.*]\s*:\s*([^%]+)/ |
| phpunit | phpunit --coverage-text --colors=never | /^\s*Lines:\s*\d+.\d+\%/ |
{{< /tab >}}
{{< tab title="Go" >}}
| Tool | Command | Regex 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" >}}
| Tool | Language | Command | Regex pattern |
|---|---|---|---|
| OpenCover | .NET | None | /(Visited Points).*\((.*)\)/ |
| dotnet test | .NET | dotnet test | /Total\s*|*\s(\d+(?:\.\d+)?)/ |
| Pester | PowerShell | None | /Covered (\d{1,3}(\.|,)?\d{0,2}%)/ |
{{< /tab >}}
{{< tab title="Elixir" >}}
| Tool | Command | Regex pattern |
|---|---|---|
| excoveralls | None | /\[TOTAL\]\s+(\d+\.\d+)%/ |
| mix | mix test --cover | /\d+.\d+\%\s+|\s+Total/ |
{{< /tab >}}
{{< /tabs >}}
{{< details >}}
{{< /details >}}
You can require specific users or a group to approve merge requests that reduce the project's test coverage.
Prerequisites:
To add a Coverage-Check approval rule:
Coverage-Check approval rule, select Enable.Coverage-Check as the Rule name.[!note] The
Coverage-Checkapproval rule requires approval when the merge base pipeline contains no coverage data, even if the merge request improves overall coverage.
You can track coverage trends for your project or group over time.
{{< details >}}
{{< /details >}}
To add a coverage badge to your project, see test coverage report badges.
When working with coverage reporting, you might encounter the following issues.
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:
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
coveragekeyword only shows a percentage in the MR widget. For line-by-line annotations in the diff, configureartifacts:reports:coverage_reportseparately.