Mastering Code Coverage Analysis for .NET Projects without Breaking the Bank

March 03, 2023#Software Development
Author image.

Philippe Vaillancourt, Senior Consultant

Code coverage analysis is a useful tool for any developer looking to build high-quality, maintainable code. It helps identify gaps in your test coverage and gives you a clear picture of which parts of your code are well-tested and which aren’t. While Visual Studio Enterprise offers a built-in code coverage feature, not everyone has access to this edition. In this blog post, we’ll cover how to set up code coverage analysis using open source packages and extensions for command-line, VS Code and Visual Studio development.

The first step in setting up code coverage analysis is to install the Coverlet package. Coverlet is an open-source code coverage library for .NET that works with a variety of test frameworks, including xUnit. Coverlet allows you to collect coverage information from the command line and generate reports that show you which lines of code, if statements, and branches are covered by your tests. To install Coverlet from the command line, navigate to your test project directory and run the commands.

dotnet add package coverlet.collector
dotnet add package coverlet.msbuild

Once Coverlet is installed, you can collect coverage information by running your tests with the flag /p:CollectCoverage=true. Like this:

dotnet test /p:CollectCoverage=true

This will print a nice table summary of your code coverage to the console. It will also generate a coverage.json file that contains all the coverage data you need. However, this file is not human-readable, so the next step is to use extensions and other packages to make this data consumable by a human.

In VS Code

The first thing we can do is install the Coverage Gutters extension in Visual Studio Code. Coverage Gutters is a popular extension that adds coverage information to the code editor’s gutter.

To make Coverage Gutters work with Coverlet, you need to run your tests again with two additional flags. The first flag, /p:CoverletOutputFormat=lcov, tells Coverlet to generate a report in the lcov format instead of the default coverage.json . The second flag, -p:CoverletOutput=./, tells Coverlet to save the report to a file called This file can be read by Coverage Gutters, allowing you to see the coverage information directly in the code editor.

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

While Coverage Gutters is a useful tool, it doesn’t give you a complete picture of your test coverage. To get more detailed information, you can install the ReportGenerator package. ReportGenerator is an open-source tool that generates HTML reports from coverage data. To install ReportGenerator, run the command below from the command line.

dotnet tool install -g dotnet-reportgenerator-globaltool

Once ReportGenerator is installed, you can generate a report by running the following command.

reportgenerator -targetdir:.coverage

This will create an HTML report in a directory called “.coverage”. You can open this report in your web browser to see detailed information about your test coverage, including which lines of code are covered, which ones are not, and where you need to focus your efforts to improve your test coverage.

In Visual Studio

Finally, if you’re using Visual Studio, you can install the Fine Code Coverage extension to get similar functionality to Coverage Gutters. Fine Code Coverage adds a window to Visual Studio that shows you which lines of code are covered by your tests. This extension works with both the Community and Professional editions of Visual Studio. All you need to do is install the extension and run your tests. Fine Code Coverage will automatically detect the coverage data and display it in the Fin Code Coverage window.

Fine Code Coverage extension

In conclusion, code coverage analysis is an essential tool for any developer looking to build high-quality, maintainable code. While Visual Studio Enterprise offers a built-in code coverage feature, there are plenty of open-source packages and extensions that allow you to get the same functionality without the need for an expensive license. By following the steps outlined in this blog post, you can set up code coverage analysis on your local machine and start improving your test coverage today.

Copyright © 2023 NimblePros - All Rights Reserved