Code Coverage¶
Guidelines¶
- Unit-test line coverage is measured and enforced on every PR in all
be-*service repos by the shared PR pipeline. A PR whose coverage falls below the repo's minimum fails theCode Quality, Build & Testcheck, which is required to merge. - The default minimum is 70%. A repo can override it by passing
minCodeCoveragein its.github/workflows/pr_workflow.yaml. - Set your repo's minimum at (or just below) its current coverage and raise it as coverage improves. Don't lower a threshold to get a PR through — split the PR or add tests instead.
- Repos with no unit tests by design (e.g. regression-test-only repos) should opt out with
runCodeCoverage: falserather than lowering the threshold.
How coverage works in CI¶
The shared pipeline be-cicd/.github/workflows/pr_pipeline_github.yml does the following on every PR:
- Runs
dotnet testwith the coverlet collector (--collect:"XPlat Code Coverage"), filtered to unit tests only (*.Test.*/*.UnitTest.*, excluding*.RegressionTest.*). - Converts the cobertura output to an HTML report with ReportGenerator and uploads it as a workflow artifact named
coverage(retained 14 days). - Extracts the whole-solution line-coverage percentage and fails the job if it is below
minCodeCoverage.
Relevant workflow_call inputs (set in each repo's pr_workflow.yaml):
| Input | Default | Effect |
|---|---|---|
runCodeCoverage |
true |
Collect coverage and enforce the threshold. Set to false to skip both (for repos with no unit tests by design). |
minCodeCoverage |
70 |
Minimum line-coverage percentage; below this the PR check fails. |
isCodeCoverageOptional |
true |
Deprecated, no effect. Historically this flag had inverted logic and setting it to false silently disabled enforcement (TECH-10). Use runCodeCoverage instead. |
To see your repo's current number: open the latest Code Quality, Build & Test job and look for the Line coverage: line in the "Print and verify line coverage threshold" step, or download the coverage artifact for the full per-class HTML report.
Run code coverage locally¶
To generate the same report locally we use coverlet.collector, which is installed as a NuGet package in the test projects.
Setup¶
- Install ReportGenerator:
dotnet tool install -g dotnet-reportgenerator-globaltool
Collect and view a report¶
-
From the folder of the
.Testproject you want to measure, run:dotnet test --collect:"XPlat Code Coverage" -
The command prints the path of the generated
coverage.cobertura.xml. Feed it to ReportGenerator:reportgenerator -reports:"<path-to>/coverage.cobertura.xml" -targetdir:"coveragereport/" -reporttypes:Html -
Open
coveragereport/index.htmlin a browser.
Notes about the future¶
- Can/should we migrate to
dotnet-coverage, the tool provided by Microsoft, instead of coverlet? - To show coverage inline in Visual Studio there is the Fine Code Coverage extension (which itself recommends
dotnet-coverageover coverlet for collection).