Adding NUnit code coverage to SonarQube using OpenCover

Posted by Maarten De Raedemaeker on 2017-11-05
Software development

In this blogpost I’ll explain how to add your NUnit code coverage to your project analysis results, by using OpenCover.
You can find a sample project you can use to try it out yourself on my github.

In this blogpost, I’ll be using NUnit as my unit testing framework.

Using OpenCover

In my solution, I’ve added the OpenCover and NUnit.ConsoleRunner nuget packages.
If I take a look in my packages folder, I can find the corresponding packages.


We are interested in the following two executables included in these packages:

  • {OpenCoverFolder}\tools\OpenCover.Console.exe
  • {NUnitConsoleFolder}\tools\nunit3-console.exe

As I explained earlier, we’re going to use OpenCover to run the NUnit console runner to measure our code coverage.

We can do it by executing the following command: (replace the variables with the real values)

{PATH_TO_OpenCover.Console.exe} "-target:{PATH_TO_nunit3-console.exe}" 
"-register:user" "-output:opencover.xml"

Let’s take a closer look at the command:

  • We run the OpenCover.Console.exe executable
  • We are telling it to use the nunit3-console.exe as it’s target using the -target parameter.
  • We use the -targetargs parameter to pass through arguments to nunit3-console.exe. (for example in my case, I would pass through -targetargs:.\Demo.Tests\bin\Debug\Demo.Tests.dll, this would tell NUnit to run all tests in this assembly.
  • We use the -register:user argument to tell OpenCover to register the agent process it uses for the current user. (otherwise you’ll get an empty code coverage result)
  • We specify the -output parameter to choose a file where the OpenCover results get written to.

When running the command, we can see NUnit running the test and the code coverage results being written.


Adding test coverage results to SonarQube

If you need a refresher on how to use the SonarQube scanner for msbuild, take a look at my previous post about getting started with SonarQube on a C# project.

The general steps we need to take when running a SonarQube scan on a .NET project are the following:

  1. Setup the SonarQube scanner for msbuild - using the begin argument (retrieve rules from SonarQube instance, inject analyzers in build pipeline)
  2. Run msbuild
  3. Use the SonarQube scanner for msbuild to generate and submit the report - using the end argument

This would give us the following corresponding commands (replace the variables with your parameters):

  1. 1
    SonarQube.Scanner.MSBuild.exe begin 


msbuild.exe /t:Rebuild {solution_name}.sln


SonarQube.Scanner.MSBuild.exe end /d:'sonar.login='{sonarqube_logintoken}'

Now we know how to generate a code coverage result file, we can make SonarQube aware of where to pick up the results.

We can do this by modifying step 1 and specifying the sonar.cs.opencover.reportsPaths property in our SonarQube scanner command like this:


SonarQube will pick up our code coverage and show it in our projects dashboard: