How can I generate accurate code coverage reports using Coverlet in .NET projects?

ANEEZA RAJPUT 0 Reputation points
2026-07-29T05:59:15.5566667+00:00

I'm looking for the best way to generate accurate code coverage reports in a .NET application using Coverlet. I want to integrate code coverage into my testing workflow and CI/CD pipeline while ensuring reliable reporting across multiple projects.

I've been reviewing the documentation and examples available at https://coverletcoverage.com/, but I'd like to know the recommended configuration, common pitfalls to avoid, and best practices for producing consistent coverage results with modern .NET applications.

Any guidance, examples, or recommendations would be appreciated.

Developer technologies | .NET | Other
0 comments No comments

2 answers

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 8,195 Reputation points Microsoft External Staff Moderator
    2026-07-29T08:03:11.44+00:00

    Hello @ANEEZA RAJPUT ,

    I see you are looking for the recommended approach to generate code coverage reports in .NET using Coverlet and integrate them into your CI/CD pipeline.

    To generate accurate code coverage reports, the recommended approach is to use the VSTest integration via the .NET CLI. The coverlet.collector NuGet package is included by default when you create new xUnit or NUnit test projects in modern .NET.

    You can generate a Cobertura formatted coverage report by running the following command from your terminal:

    dotnet test --collect:"XPlat Code Coverage"
    

    This will automatically create a TestResults directory containing a coverage.cobertura.xml file.

    CI/CD Pipeline Integration (Azure Pipelines example)

    To publish these results in Azure DevOps, use the PublishCodeCoverageResults@2 task:

    steps:
    - task: DotNetCoreCLI@2
      displayName: 'Run Tests and Collect Coverage'
      inputs:
        command: 'test'
        projects: '**/*Tests/*.csproj'
        arguments: '--collect:"XPlat Code Coverage"'
    
    - task: PublishCodeCoverageResults@2
      displayName: 'Publish Code Coverage Report'
      inputs:
        summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
    

    Best Practices:

    1. Avoid MSBuild integration if possible: While Coverlet supports an MSBuild integration (/p:CollectCoverage=true), the VSTest collector (--collect:"XPlat Code Coverage") is the recommended method for modern .NET applications, as it is more robust and standard for CI/CD pipelines.
    2. Local Viewing: To view reports locally in an HTML format, I recommend using the ReportGenerator global tool (dotnet tool install -g dotnet-reportgenerator-globaltool), which is the reporting tool recommended in the official .NET documentation.
    3. Official Documentation: Be incredibly cautious of unofficial websites. The official source of truth for generating code coverage in .NET is the Microsoft Learn code coverage documentation, which contains all the approved references and guidance for Coverlet integration.

    If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.

    Thank you. 

    Was this answer helpful?


  2. AI answer

    2026-07-29T05:59:40.9733333+00:00

    Use Coverlet through the test workflow that matches the project and pipeline setup:

    1. For standard dotnet test workflows, use the collector integration:
         dotnet test --collect:"XPlat Code Coverage"
      
      This produces a coverage.cobertura.xml file under the TestResults directory. This is a cross-platform option and fits well in build systems that rely on the .NET CLI.
    2. If the build already uses MSBuild-based test execution, use the MSBuild integration:
         dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
      
      This also produces a coverage.cobertura.xml file.
    3. For Microsoft Testing Platform-based projects, add the coverlet.MTP package and run:
         dotnet add package coverlet.MTP
         dotnet test --coverlet
      
      This generates coverage.json in the current directory by default.

    Recommended approach

    • Prefer coverlet.collector with dotnet 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> supports json, lcov, opencover, cobertura, and teamcity.
    • --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-assembly includes the test assembly in the report.
    • --coverlet-skip-auto-props skips 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.x with MTP 2.0.x
      • 18.0.x with 1.8.x
      • 17.14.x with 1.6.2 Using the latest compatible versions is recommended.

    CI/CD guidance

    In Azure Pipelines:

    • On Windows, the built-in collector can be used with DotNetCoreCLI@2:
        - task: DotNetCoreCLI@2
          inputs:
            command: test
            projects: '**/*Tests/*.csproj'
            arguments: '--configuration $(buildConfiguration) --collect "Code Coverage"'
      
      This automatically publishes coverage data to the server, and the *.coverage file 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 extra DataCollectionRunSettings arguments because the XPlat Code Coverage collector already produces a Cobertura report.

    Common pitfalls to avoid

    • Mixing different coverage collection approaches across projects without standardizing output format.
    • Expecting the built-in .coverage binary output to be directly human-readable.
    • Adding unnecessary extra collector settings when XPlat Code Coverage already 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.


    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.