-
Notifications
You must be signed in to change notification settings - Fork 16
Code Coverage Report
How to create
Simple report to have basic numbers about code coverage
-
Use PowerShell to install Coverlet Console
dotnet tool install -g coverlet.console
-
Build the project
dotnet build
-
Create a report
coverlet route to the test project dll --target "dotnet" --targetargs "test --no-bulid"
e.g.
coverlet C:\Users\user\source\repos\WhatBackend\CharlieBackend.Api.UnitTes\bin\Debug\netcoreapp3.1\CharlieBackend.Api.UnitTes.dll --target "dotnet" --targetargs "test --no-bulid"
You will see this kind of output in your Developer PowerShell window
How to create
More detailed report with a full overview about code coverage
For some cases basic numbers may be enough. However, if you want to have more detailed information to find out whіch methods are covered you need to perform a few additional steps.
-
Use PowerShell to install Coverlet Console
dotnet tool install -g coverlet.console
-
Build the project
dotnet build
-
Add NuGet package to your Test project
coverlet.collector
-
Create report xml file
dotnet test --collect:"XPlat Code Coverage"
-
Use PowerShell to install reportgenerator
dotnet tool install -g dotnet-reportgenerator-globaltool
-
Convert your report from xml to html
reportgenerator -reports:"route to xml report (generated at Report2.step 4)" -targetdir:"coverageresults" -reporttypes:Html
e.g.
reportgenerator -reports:"C:\Users\user\source\repos\WhatBackend\CharlieBackend.Api.UnitTes\TestResults\f904c22b-d1a5-4473-8c08-3bf1f0b2b440\coverage.cobertura.xml" -targetdir:"coverageresults" -reporttypes:Html
-
Go to coverageresults folder and open Index.html file.
Enjoy your report
If you want to have more accurate numbers you will need to exclude parts of the code that don't need to be covered by tests. There are two ways of doing that:
- Add attribute to your code
[ExcludeFromCodeCoverage]
- Add a statement to the script at Report1.step 3
--exclude "[*]CharlieBackend.Panel*"
(in this case whole CharlieBackend.Root namespace will be excluded. Calculeted as 100% covered.)
Report is valid on 10.10.2021
WHAT Report. (copy folder to your local drive and open Index.html file)