Coverlet Integration with xUnit in c# | Part - 1


In this blog, I am going to explain coverlet integration using xUnit in c#. 

I will covered following topics in this blog. So the topics are:

  1. Setup required for Coverlet Integration
  2. Unit Test Result
  3. Code Coverage
  4. Coverage Output
  5. Code Coverage Report Generate

Setup required for Coverlet Integration: 

So here the Coverlet doesn't require any additional setup, other than including the NuGet package in the unit test project. It integrates with the dotnet test infrastructure built into the .NET Core CLI and when enabled, will automatically generate coverage results after tests are run. 

Unit Test Result:

For unit test result, we need to execute dotnet test command in developer powershell window.

In result window, we will get, how many test files are executed? And how many total methods are executed with passed and failed?  

Code Coverage:

Code coverage is a metric that we can use to understand how well our code is covered by tests. These metrics are usually discovered by code analysis tools that run through our source code lines, and cross-reference them against the tests that hit these various code paths.

Enabling code coverage is as simple, as setting the CollectCoverage property to true.

Two things we need to process code coverage:

1. Install Coverlet.msbuild Package from nuget.

2. Command:

               dotnet test /p:CollectCoverage=true

After the above command is run, a coverage.json file containing the results will be generated in the root directory of the test project. A summary of the results will also be displayed in the terminal.

Coverage Output:

Coverlet can generate coverage results in multiple formats, which is specified using the CoverletOutputFormat property. For example, the following command emits coverage results in the opencover format:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

Supported Formats:

json (default):

command is: dotnet test /p:CollectCoverage=true /p:CoverletOutput='./result.json'

Lcov:

command is: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info

Opencover:

command is: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

Cobertura:

command is: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura

Teamcity:

command is: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=teamcity

You can specify multiple output formats by separating them with a comma (,).

The output of the coverage result can be specified using the CoverletOutput property.

command is: dotnet test /p:CollectCoverage=true /p:CoverletOutput='./result.json'


Code Coverage Report Generate:

The Coverlet MSBuild task sets the CoverletReport MSBuild item so that you can easily use the produced coverage reports. For example, using ReportGenerator to generate an html coverage report.

Command used:

 dotnet test --no-build --output bin\Debug\netcoreapp3.1 --collect:"XPlat Code Coverage";

For Installing Report Generator, Run following Command:

      dotnet tool install -g dotnet-reportgenerator-globaltool;

Run following Command for report generation:

reportgenerator -reports:'**/coverage.cobertura.xml' -targetdir:'CoverageReports' -reporttypes:'Cobertura or Html';


For Reference:



Post a Comment

0 Comments