Generate Code Coverage Report For .NET Core & .NET Framework projects

Quang Nguyen
3 min readSep 26, 2021

--

Console Code Coverage Report

In the previous post, I talked about how to set up a SonarQube server local to analyze the code quality. In this post, I’m going to talk about how to generate the code coverage report and push the report to the sonar server. So that, we can have an overview of the quality of the codebase.

Generate console code coverage report with coverlet console

There are multiple ways to working with the coverlet package. But personally, I’m more prefer the coverlet console due to these benefits:

  • No need to add an extra dependency package such as coverlet.collector or coverlet.msbuild to the codebase (test projects) => No source code modification is needed.
  • Can support both .NET Framework and .NET Core project.
  • Simple to set up and run.

Prerequisites

Installation

dotnet tool install --global coverlet.console

Usage

The coverlet tool is invoked by specifying the path to the assembly that contains the unit tests. You also need to specify the test runner and the arguments to pass to the test runner using the --target and --targetargs options respectively. The invocation of the test runner with the supplied arguments must not involve a recompilation of the unit test assembly or no coverage result will be generated.

The following example shows how to use the familiar dotnet test toolchain:

coverlet /path/to/test-assembly.dll --target "dotnet" --targetargs "test /path/to/test-project --no-build"

Note: The --no-build flag is specified so that the /path/to/test-assembly.dll assembly isn't rebuilt

See documentation for advanced usage.

Back to the age calculator project. Let’s generate a console code coverage report as an example:

coverlet .\AgeCalculatorTests.dll --target "vstest.console.exe" --targetargs ".\AgeCalculatorTests.dll" --format "opencover" --output .\coverage-reports\

Generate HTML test report with Report Generator

ReportGenerator converts coverage reports generated by coverlet, OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov or lcov into human-readable reports in various formats.

The reports do not only show the coverage quota but also include the source code and visualize which lines have been covered.

Installation

There are multiple ways to getting started with the report generator in your local machine. But, IMHO, the simplest way is:

  • Download the release as a zip package and then
  • Copy all contents in the appropriate .NET runtime version folder - which is installed on your local machine to a new folder
  • Modify the PATH variable to enable global access to the ReportGenerator.exe file

Usage

ReportGenerator "-reports:.\coverage-reports\coverage.opencover.xml" "-targetdir:.\report"

Example output

Summary report
Detailed report

Push the code coverage report to the SonarQube server

SonarScanner.MSBuild.exe begin /k:"age_calculator" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="<your_sonar_project_key_here>" /d:sonar.cs.opencover.reportsPaths=".\AgeCalculatorTests\bin\Debug\net48\coverage-reports\coverage.opencover.xml" /d:sonar.coverage.exclusions="**Program.cs"
MsBuild.exe /t:Rebuild
SonarScanner.MSBuild.exe end /d:sonar.login="<your_sonar_project_key_here>"
Code coverage overview

--

--

Quang Nguyen

Software Engineer (C#/.NET, JavaScript, Microservices, K8s, Azure)