Display Code Coverage in VS Code
Hey C# developers, have you ever wondered how thorough your unit tests are? Do you wonder if you’re written enough tests in your project?
A common way to measure your effectiveness of your unit tests is to turn on Code Coverage during your Build Pipelines (e.g. Jenkins, Azure Devops). At the end of each build, you’ll get a report that shows the percentage of code that is covered by unit tests.
The downside to this is that it requires developers to actively monitor the Code Coverage reports. Next, a developer will have to find the Class or Function that is missing tests and then write tests for it. Finally, you’ll have to push new code to your repository, and wait for the build to complete again. All this could take a few minutes to 20 minutes.
That is too long to wait. Is there a way to get quicker feedback?
In this article, I will show you how to write C# code and unit tests in Visual Studio Code, view your code coverage report without leaving VSCode at all.
To accomplish this, we’ll need our unit tests to output our Coverage Report in LCOV format. The reason this needs to be in LCOV is that we’ll also use another plugin from VSCode, called Coverage Gutters, that highlights the line of code that are not covered by tests.
The end result is something like this.
First, you’ll need a .NET Core project written in C#.
Next, create another project for unit tests. We’ll use MSTest in this example.
Now, let’s write some code without any unit tests.
In your test project, we’ll need a library called Coverlet. Coverlet is a cross platform code coverage framework for .NET, with support for line, branch and method coverage. It works with .NET Framework on Windows and .NET Core on all supported platforms. It will generate code coverage report in various formats such as HTML, XML and LCOV. For this tutorial, we’ll stick with LCOV for this example.
In your project, add the coverlet.msbuild and coverlet.collector.
Next, you’ll need a VS Code plugin called Coverage Gutters.
Run your unit test project with the following command dotnet command
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info
If you find that hard to remember and hate typing that each time to update your code coverage report, you can always create a Visual Studio Test Task.
In .vscode folder, open the task.json file and add the following task. This JSON defines a task the “dotnet test” command from the previous step to the VSCode test task.
{ "label": "test", "command": "dotnet", "group": { "kind": "test", "isDefault": true }, "args": [ "test", "/p:CollectCoverage=true", "/p:CoverletOutputFormat=lcov", "/p:CoverletOutput=./lcov.info" ], "type": "process", "problemMatcher": "$msCompile"}
After your test runs, you should see a lcov.info file in your unit test project. It is important to output your coverage report in lcov and in the file name lcov.info so that the Coverage Gutter plugin works right out of the box without additional configuration.
Now back on VSCode, toggle on Coverage Gutter. Hooray! You’re now writing tests and seeing where you’re lacking coverage in a matter of seconds/minutes.
(Don’t forget to run the Test Task each time you want to update your coverage with new results.)