Search⌘ K

How Is Code Coverage Assessed?

Explore how to assess code coverage metrics by running .NET tests with the coverlet.collector tool. Learn to interpret reports that show line and branch coverage, understand executable lines included in coverage, and how these metrics reflect the quality of your automated tests.

There are several ways of collecting code coverage metrics while running .NET tests. One common method is to install the coverlet.collector NuGet package in the test project. This package already comes preinstalled if we use the default xUnit project template.

The following playground demonstrates how the coverlet.collector collects the coverage metrics. The NuGet package reference is on lines 19–22 of the MainApp.Tests.csproj file.

namespace MainApp;

public class Calculator
{
    public int MultiplyByTwo(int value)
    {
        return value * 2;
    }
}
Code coverage demonstration

Collecting coverage metrics

To collect the coverage metrics from our tests, we can run the dotnet test command with an additional --collect parameter. The parameter’s value corresponds to the coverage collection package we use. For the coverlet.collector NuGet package, the value is "XPlat Code ...