Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
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:
- 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. - 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. - 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.