Unit Testing - .NET Code Coverage

Posted by Hon Lee on January 15, 2025

An interesting way to check against code coverage in .NET VIsual Studio without having to wait for the pipelien to run to wait for the code coverage results in unit testing is to use a tool called Coverlet.msbuild, its an intuitive tool that can run code coverage locally on your machine against the current code for unit tests and generate a set of results from the command line. Here's how to do it: -

Install .NET report generator in terminal with the command:

dotnet tool install -g dotnet-reportgenerator-globaltool

Add Coverlet MS build package from NuGet to testing project

dotnet add package coverlet.msbuild

Run dotnet tests from terminal with the command:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=lcov.info

Use code coverage for unit testing - .NET | Microsoft Learn

Here is a video using the tool Coverlet.msbuild with Nick Chapsas: -

 

Nick Chapsas .NET Code Coverage