Skip to content

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 the Code Quality, Build & Test check, which is required to merge.
  • The default minimum is 70%. A repo can override it by passing minCodeCoverage in 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: false rather 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:

  1. Runs dotnet test with the coverlet collector (--collect:"XPlat Code Coverage"), filtered to unit tests only (*.Test.* / *.UnitTest.*, excluding *.RegressionTest.*).
  2. Converts the cobertura output to an HTML report with ReportGenerator and uploads it as a workflow artifact named coverage (retained 14 days).
  3. 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

  1. Install ReportGenerator: dotnet tool install -g dotnet-reportgenerator-globaltool

Collect and view a report

  1. From the folder of the .Test project you want to measure, run:

    dotnet test --collect:"XPlat Code Coverage"

  2. 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

  3. Open coveragereport/index.html in 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-coverage over coverlet for collection).