How Is Code Coverage Assessed?
Learn how to measure code coverage with xUnit tests.
We'll cover the following...
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; } }
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
...