Lesson 93 of 93 · 5 min read · Updated
Code Coverage Reports in .NET with coverlet and ReportGenerator, Locally and in CI
On this page
A code coverage report shows which lines and branches your tests executed. Used well, it points at code that no test touches. Used badly, it becomes a number that teams chase while the real risks stay untested. This lesson shows how to collect coverage with coverlet, turn it into a readable report with ReportGenerator, publish the summary in GitHub Actions, and read the result as a gap-finding tool.
Technical Requirements
- .NET 11 SDK
coverlet.collector10.1.1 in every test project- The
dotnet-reportgenerator-globaltool.NET tool - A GitHub Actions workflow, if you want the summary in CI
Collect Coverage Data with coverlet
Coverage collection starts in the build setup. A shared Directory.Build.props adds the test packages to every project whose name ends in .Tests, including the coverlet data collector:
Modules/Directory.Build.props
Because the reference lives in one file, a new test project gets coverage support automatically. Nobody has to remember to add the package.
With the collector in place, one option on dotnet test turns collection on:
XPlat Code Coverage is the name of coverlet's data collector. It works the same on Windows, Linux and macOS. For each test project, it writes a coverage.cobertura.xml file into a TestResults folder with a random GUID subfolder. Cobertura is an XML format that most coverage tools can read, but it is not meant for humans.
Turn the Raw Data into a Report
ReportGenerator merges all Cobertura files into one report:
The -reports glob picks up the files from every test project, so the report shows the whole solution, not one project at a time. The HTML report lets you drill down from assemblies to classes to individual lines, with covered lines in green and uncovered lines in red. Branch coverage is shown too: a line with an if can be executed while one of its two branches never runs. (open is the macOS command; on Windows use start, on Linux xdg-open.)
Publish the Coverage Summary in CI
The fast job of the CI workflow collects coverage during its test step and then generates three report types:
.github/workflows/modules-ci.yml
Each report type has a job. MarkdownSummaryGithub produces SummaryGithub.md, and appending it to $GITHUB_STEP_SUMMARY shows a coverage table directly on the workflow run page, so nobody has to download anything to see the numbers. TextSummary is a plain text overview that is easy to read in the log. The full Html report is uploaded as an artifact together with the TRX test results, for anyone who wants to look at individual lines.
The summary step uses if: always() and ends with || true. The report step itself has no if: always(), so when a test fails it is skipped and no summary file exists. || true then lets the cat fail quietly, and the run shows the real failure, the failing test, instead of a second error. The upload step also runs on failure, so the TRX files are always available.
Pitfall: a coverage report only covers the tests that ran in that job. In this workflow the fast job filters out the container tests, so code that only the Testcontainers and Aspire tests exercise does not count in this report. A low number for that code is not a missing test; it is a test that runs in another job. Always check which tests produced a report before you draw conclusions from it.
Read Coverage as a Gap Finder, Not a Grade
Coverage tells you what your tests executed. It does not tell you what they checked. A test that calls a method and asserts nothing produces exactly the same coverage as a test with careful assertions. Two test suites can show the same percentage while one catches regressions and the other catches nothing.
So use the report to ask questions:
- Which red lines are business rules, error handling or edge cases? Those are the gaps worth closing.
- Which red lines are trivial, such as generated code or simple property getters? Those can stay red.
- Did a recent change add a whole new uncovered class? That is worth a conversation in the pull request.
Common mistake: setting a hard target such as "80 percent or the build fails" and treating it as a quality measure. Teams then write tests that execute code without verifying behaviour, just to move the number. If you want to know whether your tests really detect bugs, mutation testing answers that question: it changes your code on purpose and checks whether any test fails.
Summary
- Add
coverlet.collectoronce inDirectory.Build.props, so every test project can collect coverage. - Run
dotnet test --collect:"XPlat Code Coverage"to produce Cobertura files per test project. - Use ReportGenerator to merge them into an HTML report and a Markdown summary.
- In GitHub Actions, append the summary to
$GITHUB_STEP_SUMMARYand upload the HTML report as an artifact. - Treat coverage as a tool for finding untested code, not as a grade for test quality.