Skip to content
Latchkey

dotnet test --collect: Code Coverage in CI

dotnet test --collect "XPlat Code Coverage" runs the coverlet data collector and writes a Cobertura coverage report alongside the test results.

Coverage in CI is a data collector plus a report generator. Getting the collector wired and the output path right is the whole job.

What it does

The "XPlat Code Coverage" collector (provided by coverlet.collector, which the test project must reference) instruments the assemblies during dotnet test and writes a coverage.cobertura.xml under the results directory. A separate tool like ReportGenerator turns that into HTML or a summary.

Common usage

Terminal
dotnet test --collect "XPlat Code Coverage" \
  --results-directory ./coverage
# turn the cobertura xml into a report
dotnet tool run reportgenerator \
  -reports:./coverage/**/coverage.cobertura.xml \
  -targetdir:./coverage/report

Options

FlagWhat it does
--collect "XPlat Code Coverage"Run the coverlet cross-platform collector
--results-directory <dir>Where the coverage and trx files are written
--settings <runsettings>A .runsettings to tune coverage (excludes, formats)
--no-buildReuse an existing build
-- DataCollectionRunSettings...Inline collector configuration

In CI

Reference coverlet.collector in the test project or the collector is not found and no coverage is produced. The report lands in a per-run GUID subfolder of --results-directory, so glob **/coverage.cobertura.xml when feeding it to a report tool or a coverage service. Install ReportGenerator as a local tool and run it via dotnet tool run.

Common errors in CI

A run that produces a .trx but no coverage.cobertura.xml means the coverlet.collector package is not referenced by the test project, so the collector silently did nothing. "Data collector 'XPlat Code Coverage' message: ... could not be loaded" means a version mismatch between the collector and the test SDK. An empty report often means the collector instrumented the test assembly but not the assembly under test; check the include/exclude filters in .runsettings.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →