Code Coverage in .NET Core Projects

If you have created a unit test project in .NET Core, then you must have noticed a NuGet package, coverlet.collector, is installed in the default project templates for MSTest and xUnit. What does this NuGet package do?

The coverlet documentation (link) tells us that coverlet is a cross platform code coverage framework for .NET, with support for line, branch and method coverage. It means that we are able to check the code coverage for .NET Core applications using coverlet.

Creating the Coverage Report

Install the NuGet package coverlet.msbuild to the MSTest project using the following command.
dotnet add package coverlet.msbuild
The pre-installed NuGet package coverlet.collector doesn’t work with the dotnet test command. Thus, we need to install the coverlet.msbuild package in order to generate coverage reports in the CI pipeline. This NuGet package integrates coverlet with the .NET build system, so that coverlet will compute code coverage after tests.

Run  dotnet test command to generate a coverage report.

dotnet test /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov

Several parameters are passed into the dotnet test command. The first one, CollectCoverage=true, means we want to collect code coverage.

The second parameter, CoverletOutput, specifies the output file destination, which is in the TestResults folder. The most commonly available .gitignore file for .NET projects sets the TestResults folder to be ignored for version control. Thus, we will put testing related things into the TestResults folder, so that they won’t be accidentally committed to our code repository if coverage reports are generated locally.

The third parameter, CoverletOutputFormat, indicates the report file format, which is lcov in this case. The coverlet supports many formats, such as opencoverjsonlcovcoberturateamcity, and so on. In this project, we will stick with the lcov format, because the GitHub Actions for coveralls.io uses the lcov format.

If we run the command, then we will see the Console output like the following screenshot.

The Console output of dotnet test with coverlet

The Console output writes out the generated coverage report file path and the coverage metrics, which give us a glimpse of the coverage rates. The generated coverage.info file contains much more detailed information on the line hits, but it is not so readable as compared to the reports after parsing and transformation by specialized tools. We will use an online report service from coveralls.io to visualize the code coverage.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: