First, install the Coverlet collector as a NuGet package in your test project.
dotnet add package coverlet.collectorEnsure that code coverage is enabled in your project. If you’re using the .NET CLI, you can include the --collect argument to enable the Coverlet collector:
dotnet test --collect:"XPlat Code Coverage"After running your tests with code coverage enabled, a coverage report will be generated in the TestResults folder. This report is usually in .cobertura format or .json format or in .xml format.
To view the line coverage, you can either open the generated report in a code coverage analysis tool or convert the report into a more readable format. You can use tools like:
- ReportGenerator to convert the coverage report into HTML:
Then, open the
dotnet tool install -g dotnet-reportgenerator-globaltool reportgenerator -reports:TestResults/**/*.cobertura.xml -targetdir:coveragereport
index.htmlfile in thecoveragereportfolder to view the coverage report.