Code Coverage Report - ita-social-projects/WhatBackend GitHub Wiki

Report 1 (Simple)

How to create


Simple report to have basic numbers about code coverage

  1. Use PowerShell to install Coverlet Console

    dotnet tool install -g coverlet.console

  2. Build the project

    dotnet build

  3. 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

test

Report 2 (Detailed)

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.

  1. Use PowerShell to install Coverlet Console

    dotnet tool install -g coverlet.console

  2. Build the project

    dotnet build

  3. Add NuGet package to your Test project

    coverlet.collector

  4. Create report xml file

    dotnet test --collect:"XPlat Code Coverage"

  5. Use PowerShell to install reportgenerator

    dotnet tool install -g dotnet-reportgenerator-globaltool

  6. 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
    
  7. Go to coverageresults folder and open Index.html file.

    Enjoy your report

test

How to exclude things from 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:

  1. Add attribute to your code [ExcludeFromCodeCoverage]
  2. 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.)

WHAT project Code Coverage Report

Report is valid on 10.10.2021

WHAT Report. (copy folder to your local drive and open Index.html file)