Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
Use Coverlet through the test workflow that matches the project and pipeline setup:
- For standard
dotnet testworkflows, use the collector integration:
This produces adotnet test --collect:"XPlat Code Coverage"coverage.cobertura.xmlfile under theTestResultsdirectory. This is a cross-platform option and fits well in build systems that rely on the .NET CLI. - If the build already uses MSBuild-based test execution, use the MSBuild integration:
This also produces adotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=coberturacoverage.cobertura.xmlfile. - For Microsoft Testing Platform-based projects, add the
coverlet.MTPpackage and run:
This generatesdotnet add package coverlet.MTP dotnet test --coverletcoverage.jsonin the current directory by default.
Recommended approach
- Prefer
coverlet.collectorwithdotnet test --collect:"XPlat Code Coverage"for modern cross-platform .NET test workflows. - Use Cobertura output when the report needs to be published in CI/CD systems.
- Use a report generator to turn collected coverage data into human-readable reports such as HTML.
Useful Coverlet options with Microsoft Testing Platform
-
--coverlet-output-format <format>supportsjson,lcov,opencover,cobertura, andteamcity. -
--coverlet-include <filter>and--coverlet-exclude <filter>control which assemblies are measured. -
--coverlet-exclude-by-file <pattern>excludes files by glob pattern. -
--coverlet-exclude-by-attribute <attribute>excludes code marked with specific attributes. -
--coverlet-include-test-assemblyincludes the test assembly in the report. -
--coverlet-skip-auto-propsskips auto-implemented properties.
Best practices for consistent results
- Standardize on one collection mode across projects in the same solution or pipeline.
- Generate Cobertura output when the pipeline needs to publish coverage results.
- Use include and exclude filters deliberately so coverage reflects the actual system under test instead of test infrastructure or generated code.
- Keep package versions current. For Microsoft Testing Platform compatibility, the documented combinations are:
-
Microsoft.Testing.Extensions.CodeCoverage 18.1.xwithMTP 2.0.x -
18.0.xwith1.8.x -
17.14.xwith1.6.2Using the latest compatible versions is recommended.
-
CI/CD guidance
In Azure Pipelines:
- On Windows, the built-in collector can be used with
DotNetCoreCLI@2:
This automatically publishes coverage data to the server, and the- task: DotNetCoreCLI@2 inputs: command: test projects: '**/*Tests/*.csproj' arguments: '--configuration $(buildConfiguration) --collect "Code Coverage"'*.coveragefile can be downloaded from the build summary for viewing in Visual Studio. - For Linux or macOS, use Coverlet.
- To publish coverage results to the server, configure the coverage tool to generate Cobertura or JaCoCo format, then publish those results. When using
coverlet.collector, avoid adding extraDataCollectionRunSettingsarguments because theXPlat Code Coveragecollector already produces a Cobertura report.
Common pitfalls to avoid
- Mixing different coverage collection approaches across projects without standardizing output format.
- Expecting the built-in
.coveragebinary output to be directly human-readable. - Adding unnecessary extra collector settings when
XPlat Code Coveragealready emits Cobertura. - Using incompatible Microsoft Testing Platform and code coverage package versions.
If a readable report is needed after collection, use a report generator on the Cobertura output. Coverlet collects the coverage data; report generation is a separate step.