Code coverage in .NET 6

April 29th 2022 Unit Testing .NET

Since .NET 5, the coverlet.collector NuGet package is pre-installed in the test project templates, which can generate code coverage reports when the tests are executed. Let us take a look at how you can use this in your code editor.

There is a built-in code coverage feature in Visual Studio 2022, but unfortunately it is only available in the Enterprise edition. If you are using Visual Studio 2022 Enterprise, you can create a code coverage report in one of two ways:

  • via the Test > Analyze Code Coverage for All Tests menu item,
  • via the Analyze Code Coverage context menu item for tests in the Test Explorer window.

You can then view the report in the Code Coverage Results tool window:

Code Coverage Results window in VS 2022

Using the Show Code Coverage Coloring button in the toolbar of this window, you can also view the results by coloring the code in the code editor window:

Code coverage coloring in VS 2022

If you do not have Visual Studio 2022 Enterprise, you can install the Fine Code Coverage extension instead. With this, you do not need to run a special command to get the coverage report. Simply run the tests from the Test Explorer window and open the Fine Code Coverage tool window to see the results:

Fine Code Coverage window in VS 2022

The results are also automatically displayed in the gutter of the code editor window:

Coverage results in code editor gutter in VS 2022

If you are using Visual Studio Code, then you should install the following extensions:

To make them work with Coverlet, you must include the following settings in the .vscode/settings.json file:

{
  "dotnet-test-explorer.testArguments": "--collect:\"XPlat Code Coverage\"",
  "coverage-gutters.coverageFileNames": ["coverage.cobertura.xml"]
}

They do the following:

  • Instruct .NET Core Test Explorer to add --collect: "XPlat Code Coverage" to the dotnet test call to collect coverage results when the tests are run.
  • Set the filename for the Coverage Gutters from which to read the coverage results.

After you have done this (and run the test for the first time), you should be able to enable Coverage Gutters visualization by clicking the Watch button in the status bar. This will add the code coverage visualization to the code editor gutter and display the coverage level for the current file in the status bar:

Coverage results in code editor gutter in VS 2022.png

If you prefer to visualize the code coverage in the ruler on the right or color the line instead (or in addition), you can configure the following settings accordingly:

{
  "coverage-gutters.showLineCoverage": true,
  "coverage-gutters.showRulerCoverage": true,
  "coverage-gutters.showGutterCoverage": true
}

.NET has made great progress in many respects. Code coverage is one of them. The project templates are already fully preconfigured for it. Depending on which code editor you use, you only need to install and configure the right extensions to fully integrate code coverage into your development loop.

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License