In this article I’ll take you through setting up and your development environment so that you can easily see what code is covered by your unit tests which ones aren’t, and view some statistics too. Normally this requires Visual Studio Enterprise which for many developers and businesses is prohibitively expensive, but here I’ll show you how to do this on any edition of Visual Studio (I’m using 2019), including the completely free community edition using tools that are freely available.
The three extensions we will use
To set up our dev environment we’re going to use the following
- Report Generator
- Run Coverlet Report by me
Setting up Report Generator
Report Generator is a free tool to take code coverage output files and generate reports from them. The reports are HTML files that are interactive in that they allow you to drill down through them until you see actual code lines with coverage indicators.
For more about Report Generator see this page.
To keep things simple and to make this work on any of our Visual Studio solutions we’re going install ReportGenerator as global tool, this will allow us to run ReportGenerator as though it were a standard console application (though we won’t actually need to do this manually as you will see).
To install ReportGenerator, open up a new command prompt and type the following
dotnet tool install -g dotnet-reportgenerator-globaltool
If everything works you should see an output that is something like
And just to prove everything is working you can open another command prompt and type
You should see a version number followed by the different command line parameters that you can pass to it.
Installing Run Coverlet Report
For now we will skip installing Coverlet and come back to that later.
Run Coverlet Report is a Visual Studio extension that I wrote to run unit tests using the dotnet command line, with the correct Coverlet settings so that the tests output information that is then fed into ReportGenerator. I didn’t like having to remember all the command line switches so this extension makes it simple to run the necessary steps.
Open up Visual Studio 2019 and find the Run Coverlet Report extension in the extension manager and install it.
For more information about Run Coverlet Report see this page.
Coverlet is a cross platform, open source Github project that allows us to collect code coverage statistics from our unit tests in various formats. There are quite a few ways of using Coverlet including using Coverlet and ReportGenerator together in Azure build pipelines.
For more information about Coverlet see this page.
Not we need to install Coverlet. Unlike the first two tools we installed, Coverlet works as a nuget package which we install into our unit test projects. In this article I’m going to be using Coverlet.Collector which I believe is the recommended way to use Coverlet (Run Coverlet Report will also work with Coverlet.MSBuild, if you are doing this refer to the documentation on the Run Coverlet Report and Coverlet Github pages).
First you need to decide if you need to do this step, currently if you are creating xUnit unit test projects then a reference to Coverlet.Collector is already added to your project dependencies so all you should do is update to the latest version.
If you are using a different unit test framework then find and add the nuget package yourself to all of your unit test projects. Here’s the nuget package you’re looking for. If you miss any unit test projects then those projects will not be included in your coverage output.
Viewing Code Coverage
Now it’s time to put these tools to work, but first I’m going to assume that you have some unit tests in your solution, if you don’t then you’re going to need to go and write some and then come back…
…right, time to see some code coverage. We have all the tools setup so this is a pretty simple process.
First of all, check that the solution builds and run the unit tests normally, making sure they all pass.
Next on the Tools menu you will see that there are two new commands that the Run Coverlet Report extension has added.
Click on Tools | Run Code Coverage
There will be a short delay while Run Coverlet Report starts running your unit tests and you will see a couple of command windows will be briefly shown on screen. These command windows are actually the output from the three tools and if something goes wrong then errors are usually shown in these windows to give you some clues and help you figure out what has gone wrong.
What’s actually happening at this point is that Run Coverlet Report is sending commands to dotnet, Coverlet and ReportGenerator and coordinating them so that the output from one is the input into the next.
Finally If everything works fine the command windows will disappear and Run Coverlet Report will open the report that ReportGenerator has created right inside a Visual Studio window so that you don’t have to leave your development environment. This is the code coverage report for your solution.
The first page of the report is an overview for your solution, it gives a high level view and you can click the links in the report to drill down and eventually see which lines of code are covered or not, as shown below.
Run Coverlet Report will also highlight lines in your code files. To see these highlights just open a code file and one of three colours will be overlaid onto your code to indicate if that line is covered, uncovered or partially covered.
If you don’t see the the highlights or you want to toggle them on and off then you can click on the second menu item that Run Coverlet Report added to the Tools menu, just click Tools | Toggle Code Coverage Highlighting.
Finally if the highlights don’t look nice on your colour scheme then you can tweak the colours and change some of the integration settings (for example if you are using Coverlet.MSBuild) by going into the Visual Studio options and finding the Run Coverlet Report section. Here you can turn on/off the border and fill of the highlights, change them between solid and gradient colours and change the colours themselves by entering new values that you can capture from pretty much any colour picker.
Other settings on this page are used for more advanced features such as excluding certain assemblies from the test run, disabling nuget restore and using Coverlet.MSBuild instead of Coverlet.Collector. I would advise you to look at the Run Coverlet Report Github page for more information on these settings.
Wrapping It Up
That’s it for this article, I hope I’ve managed to show you how simple it can be to collect code coverage without buying a license for Visual Studio Enterprise Edition and that by having code coverage available to you within your IDE it helps you in your day to day TDD work.
Have fun coding.