DEV Community

Miguel Teheran
Miguel Teheran

Posted on

Coverlet, unit tests coverage for .NET

Measuring the coverage of unit tests is an important topic because we need to check the design of the tests and the scope in the code of them.

Coverlet is an amazing tool to measure the coverage of unit tests in .NET projects.

Coverlet is completely opensource and free. it supports .NET and .NET Core and you can add it as a NuGet Package.

GitHub logo tonerdo / coverlet

Cross platform code coverage for .NET

Coverlet

Build Status

Coverlet is a cross platform code coverage framework for .NET, with support for line, branch and method coverage. It works with .NET Framework on Windows and .NET Core on all supported platforms.

Installation

VSTest Integration:

dotnet add package coverlet.collector

N.B. You MUST add package only to test projects

MSBuild Integration:

dotnet add package coverlet.msbuild

N.B. You MUST add package only to test projects

Global Tool:

dotnet tool install --global coverlet.console

Quick Start

VSTest Integration

Coverlet is integrated into the Visual Studio Test Platform as a data collector. To get coverage simply run the following command:

dotnet test --collect:"XPlat Code Coverage"

After the above command is run, a coverage.cobertura.json file containing the results will be published to the TestResults directory as an attachment. A summary of the results will also be displayed in the terminal.

See documentation for advanced usage.

Requirements

  • You need

First, we have to add the NuGet within an existing unit test project (MSTest, xUnit, etc..). Coverlet has many ways to use it but I recommend to use MSBuild.

dotnet add package coverlet.msbuild

after that, we can use easily the integration between MSBuild and coverlet to run the test and measure the coverage with the following command:

dotnet test /p:CollectCoverage=true

you will get the following result:

Alt text of image

In the first column from left to right we can see the list of modules covered. In the column 'Line', we get the percentage of lines checked after running the tests and it's the same for 'Branch'(statements) and 'Method'(functions inside the classes).

Coverlet generates a file coverage.json that contains the whole information displayed in the console. You can consume this file with your own application.

Try it out!

GitHub logo Mteheran / CoverletDemo

Coverlet demo for unit test coverage

CoverletDemo

Coverlet demo for unit test coverage




Top comments (4)

Collapse
 
fabasoad profile image
Yevhen Fabizhevskyi

Thanks for this article. Trying to connect coverlet test coverage to my CI pipeline now but didn't get how to collect total test coverage. I have 3 test projects, like Project1.Tests.csproj, Project2.Tests.csproj and Project3.Tests.csproj and coverlet shows tests coverage for each project separately. Wondering is there a possibility to collect total test coverage between all projects.

Collapse
 
mteheran profile image
Miguel Teheran

Coverlet always shows the namespaces separated but you also will see a total table at the end. you have to put the test dlls (binaries) in the same folder and run "dotnet test /p:CollectCoverage=true" there.

Collapse
 
fabasoad profile image
Yevhen Fabizhevskyi

Thanks, it is what I need actually! Also, I have added a few more command line arguments. Here is the command that works for me perfectly (just in case if anyone needs it):

dotnet test ./MySolution.sln /p:CollectCoverage=true /p:MergeWith=$(pwd)/coverage/coverage.json /p:CoverletOutput=$(pwd)/coverage/ "/p:CoverletOutputFormat=\"json,opencover\""
Thread Thread
 
mteheran profile image
Miguel Teheran

Sweet! Thanks for sharing it