quality-test-implementation
Use when raising code quality or test coverage across a .NET, Java, or Python repository.
Documentation & Preview
.NET Test Coverage
Target: 90% line and branch coverage. Works for xUnit, NUnit, and MSTest — all three run through dotnet test, so the coverage command is identical regardless of framework.
Prerequisites
Microsoft.NET.Test.Sdkreferenced by the test project.- A coverage collector. Two options:
- XPlat Code Coverage (recommended, zero extra packages): built into
Microsoft.NET.Test.Sdkvia the Coverlet collector. - Coverlet.Console (standalone):
dotnet tool install --global coverlet.console.
Run coverage
XPlat (built-in, no extra tooling)
# Collect coverage into ./TestResults
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResultsOutput: TestResults/<guid>/coverage.cobertura.xml.
Enforce a threshold
dotnet test /p:CollectCoverage=true /p:CoverageFormat=cobertura /p:Threshold=90 /p:ThresholdType=line,branchCoverlet.Console (explicit DLL)
coverlet <TestProject>/bin/Debug/net8.0/<TestProject>.dll \
--target "dotnet" \
--targetargs "test --no-build" \
--format cobertura \
--threshold 90HTML report (reportgenerator)
# install once
dotnet tool install --global dotnet-reportgenerator-globaltool
# generate from the XPlat result
reportgenerator \
-reports:"TestResults/**/coverage.cobertura.xml" \
-targetdir:"CoverageReport" \
-reporttypes:"Html;Cobertura"Open CoverageReport/index.html.
Common mistakes
| Mistake | Symptom | Fix | |---|---|---| | Missing Microsoft.NET.Test.Sdk | --collect:"XPlat Code Coverage" produces no XML | Add the package to the test project | | Running coverage on the wrong config | Lower numbers than expected | Build Release or set /p:Configuration=Release | | No threshold gate | Coverage silently regresses | Add /p:Threshold=90 (build fails below target) |