“ReportGenerator converts coverage reports generated by coverlet, OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov, or lcov into human readable reports in various formats. The reports show the coverage quotas and also visualize which lines of your source code have been covered.” - https://www.nuget.org/packages/dotnet-reportgenerator-globaltool
ReportGenerator tool wont replace Sonarqube as its not a Clean as You Code type methodology but if you want to locally and quickly check code coverage using generated reports, its a winner.
Simple Demo
These steps are for a .Net Core application but you can apply them to any code base that has coverlet.collector installed in the tests project.
Clone https://github.com/carlpaton/CoverageReportGeneratorDemo to
c:\dev\CoverageReportGeneratorDemo
, this is a net8.0 SDK-style project.Check that you can build it
1 | c:\dev\CoverageReportGeneratorDemo |
- Install the .NET Core version of ReportGenerator - dotnet-reportgenerator-globaltool, here Im using the path
tools
which is excluded in .gitignore
1 | dotnet tool install dotnet-reportgenerator-globaltool --tool-path tools --version 5.3.9 |
- Set the cover format and ensure you have
coverlet.collector
installed.
CoverageReportGeneratorDemo already has coverlet.collector 6.0.0 installed, it came with the scaffold template from Microsoft when I created the test project and selected xUnit Test Project
As mentioned in at the top coverlet.collector
can generate coverage results in multiple formats. The usual opencover format is no longer maintained 😔 and when I tested with the default json
format this resulted in no coverage, perhaps there is more settings I missed. 🤷
I next tested with cobertura in my .runsettings file and this worked, so I rolled with it.
1 |
|
- Run the tests specifying the .runsettings file
1 | dotnet test --settings="./Cover.Tests/.runsettings" |
This then results in an attachment coverage.cobertura.xml:
C:\dev\CoverageReportGeneratorDemo\Cover.Tests\TestResults\10e130bb-7829-4a36-bef3-bf16119422c8\coverage.cobertura.xml
1 |
|
You dont have to use the runsettings file, you can instead just pass collect="XPlat Code Coverage"
, where XPlat
just means cross platform, the resulting file is coverage.cobertura.xml
, I thought it would be coverage.json
but it wasnt.
1 | dotnet test --collect="XPlat Code Coverage" |
If you have other test projects you can use filter, here Im filtering where the project name is like UnitTests
1 | c:\dev\CoverageReportGeneratorDemo |
- Run the reportgenerator tool passing coverage report and target directory as arguments
1 | ./tools/reportgenerator.exe ` |
- A html report is then generated in
c:\dev\CoverageReportGeneratorDemo\report
which shows what paths have coverage and which do not, the attribute ExcludeFromCodeCoverage is supported out of the box. Previously Ive had to install coverlet.msbuild to get this to work.
You can see my generated html report here