Coverlet, unit tests coverage for .NET

Miguel Teheran - Sep 18 '19 - - Dev Community

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




. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .