What is SonarQube

What is OpenCover

OpenCover is a code coverage tool.It allows us to run an application using the tool, and it will monitor what parts of the code are executed during the execution.We can use it to run our unit test framework, and it will tell us how much of our code is covered by our tests.

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)

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.