May 14, 2018 by Anuraj
.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
Here is the command for the same.
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.
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.
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.
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.
This will help you to show code coverage also in the SonarCloud dashboard.
Happy Programming :)
Copyright © 2024 Anuraj. 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. Powered by Jekyll. Hosted with ❤ by GitHub