Unlike other application frameworks .NET Core do not provide code coverage reports out of the box, even the code coverage support was provided only in dotnet core version 2.1. But the ‘Code Coverage Analysis’ is provided with Visual Studio Enterprise edition. You can find more information here.

That’s okay, we’ve other tools available to get the code coverage reports and here I’ll be briefly explaining about it. There is already a blog post about this topic by Gunnar Peipman. You can refer that as well. Here I’ll be using a different approach which actually suited my web application. Without further ado, let’s get started.

I’ve published a copy of this article in my personal space.

Contents

Prerequisites

You need to install/Configure:

Dotnet core version 2.1 or above.

Visual Studio 2017 or above / Visual studio code.

Daniel Palme’s ReportGenerator

Microsoft.CodeCoverage

Creating dotnet application and adding test project

You can make use of dotnet new command or Visual Studio templates for creating a new project and test project. Here I’ll be covering MSTest, you can try with another unit testing framework as well.

If you’ve a dotnet core application with version 1.x, you can do the following steps to get the code coverage.

• Upgraded test related nuget refernces to latest version

<PackageReference Include=”Microsoft.NET.Test.Sdk” Version=”15.9.0" />

<PackageReference Include=”MSTest.TestAdapter” Version=”1.3.2" />

<PackageReference Include=”MSTest.TestFramework” Version=”1.3.2" />

• Add the following config in *.csproj files. More information is available here.

<DebugType>Full</DebugType>

Running tests with CLI

Once we’ve created our application and added sufficient unit tests. We need to run these tests and get the code coverage info. You do so by running the following command.

dotnet core test command

But I had a problem, dotnet was analyzing my dependent libraries as well. I was using Moq for mocking and GenFu to generate random test data. So I had to exclude these dlls during the code coverage analysis by creating a runsettings file and add the following configuration:

Config for excluding dlls in .runsettings

Include and exclude nodes use regular expressions. More information is available here.

Now we need to run test command using this .runsettings file.

dotnet test using .runsettings file

This will generate *.coverage file inside a folder whose name corresponds to a GUID. Right now dotnet core CLI do not support custom name for *.coverage file.

Convert *.coverage file to *.coveragexml file

CodeCoverage.exe is a tool that comes with the installation of Visual Studio. To generate a coverage report with ReportGenerator the file has to be converted to xml format.

To get the xml file you can use the following command:

Generating coveragexml from coverage file

Generate Reports using ReportGenerator

We need to run another command using the installed ReportGenerator.dll.

This will generate reports in *.htm format in the given output folder. If you open the index.htm file you can view the report.

Sample Report Image. Source: https://github.com/danielpalme/ReportGenerator

Powershell script with all the steps

Conclusion

It took some time for me to figure it out as lot of information regarding this was scattered all over the internet. Finally it was worth the effort and coverage reports really improve quality of unit tests and helps developer to write better unit tests. Hope this blog post will be helpful for dotnet developers treading the same path.