Code coverage in .NET Core with Coverlet

Posted by Anuraj on Monday, May 14, 2018 Reading time :1 minute

.NET Core CodeCoverage Coverlet

Few days back I wrote a post about code coverage in ASP.NET Core. In that post I was using Visual Studio 2017 Enterprise, which doesn’t support Linux or Mac and it is costly. Later I found one alternative, Coverlet - Coverlet is a cross platform code coverage library for .NET Core, with support for line, branch and method coverage. Coverlet integrates with the MSBuild system, so it doesn’t require any additional setup other than including the NuGet package in the unit test project. It integrates with the dotnet test infrastructure built into the .NET Core CLI and when enabled, will automatically generate coverage results after tests are run.

To enable code coverage, you need to run the dotnet test command with CollectCoverage property with value true. It supports multiple coverage formats, like json (default),Icov, opencover, cobertura. In this post I am using OpenCover format.

Here is the command for the same.

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

Code coverage Console output

For viewing the coverage output in detail, you can use ReportGenerator, which converts XML reports generated by OpenCover, PartCover, dotCover, Visual Studio, NCover or Cobertura into human readable reports in various formats. So once you collected the coverage information, you can run the ReportGenerator tool and get the visualization.

First you need to install the Report Generator, you can do this using Install-Package ReportGenerator -Version 4.0.0-alpha4 command. Once it is installed, you can run the following command to generate the reports.

dotnet $(UserProfile)\.nuget\packages\reportgenerator\4.0.0-alpha4\tools\netcoreapp2.0\ReportGenerator.dll" "-reports:.\coverage.xml" "-targetdir:D:\Reports"

Now you can open your reports folder and browse the index.htm file, which will show the consolidated report and you can drill down on each file to view the coverage. Here is my code coverage details for Feature Toggle project.

Code coverage Report

Coverlet allows you to specify a coverage threshold below which it fails the build. This allows you to enforce a minimum coverage percent on all changes to your project. In the project overall coverage is 75%, I am setting the threshold to 80, and here is the result.

Code coverage with Threshold

If you are using a tool like SonarCloud, you can include the coverage information as well. Here is the command, which uploads the test code coverage to SonarCloud.

dotnet "SonarScanner.MSBuild.dll" begin /k:"FeatureToggle.Core" /d:sonar.cs.opencover.reportsPaths="coverage.xml" /d:sonar.test.exclusions="test/**"

This will help you to show code coverage also in the SonarCloud dashboard.

SonarCloud dashboard with Code Coverage

Happy Programming :)

Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon

BMC logoBuy me a coffee

Copyright © 2018 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub