Unit Test Coverage Metrics on Conditions, branches and Others

I used VS 2019 enterprise addition to get some basis unit test coverage stats (line and block) into SonarCloud. How do I get more metrics, such as Conditions, branches and others into SonarCloud?

Hey there.

If you’re successfully importing coverage information into SonarCloud already, you are probably importing everything thing your coverage tool supports with regard to condition/branch coverage. What information do you see as missing?

Yes, I think you are correct. I am using Visual Studio Enterprise 2019 addition, while has line and block information. I have not found condition and branch stats in VS Enterprise 2019. Do you have a recommendation on a coverage tool that provides provides line, block, function, condition, and branch coverage stats?

Hey there.

No specific recommendations to give – we currently support the following code coverage tools: VsTest, dotCover, OpenCover and NCover3 (deprecated). Out of these, VsTest and dotCover do not support branch coverage.

Thank you very much for your help. I will search for and review the documentation for using OpenCover. Once I have an understanding of it, I will search for and review the documentation for integrating it with SonarCloud. Thanks agian for your help.

Charlie Teague

1 Like

If anyone has used openCover to get branch coverage to display in SonarCloud, then I am interested in knowing how you did it.

In order to get branch unit test coverage in sonar cloud, I have installed OpenCover locally. I followed the installation instructions on the following page:

So, I installed the following nuget packages:

  • OpenCover
  • ReportGenerator
  • NUnit.Runners

I created a file called testrunner.bat with the following line in it:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" C:\src\dev\cloudaio\Tests\CloudApi.NUnit.UnitTests\bin\Debug\*.dll

Then I ran each of the following in commandline:

C:\src\dev\cloudaio\Tests\CloudApi.NUnit.UnitTests\bin\Debug>C:\src\dev\cloudaio\packages\OpenCover.4.7.922\tools\OpenCover.Console.exe -target:"C:\Users\CharlesTeague\OneDrive - P97\Desktop\code-coverage\sonarcloud\opencover\testrunner.bat" -register:user

C:\src\dev\cloudaio\Tests\CloudApi.NUnit.UnitTests\bin\Debug>C:\src\dev\cloudaio\packages\ReportGenerator.4.8.7\tools\net5.0\ReportGenerator.exe -reports:results.xml -targetdir:coverage

Then open the following shows no coverage which is not right: C:\src\dev\cloudaio\Tests\CloudApi.NUnit.UnitTests\bin\Debug\coverage\index.html

I also downloaded OpenCover and run it on the solution with similar commands. The unit tests executed. The coverage report only showed a non-zero coverage on one of the projects, and zero on all of the rest. There are many projects that have many unit tests that it reported 0% coverage.

Has anyone used SonarCloud with any test coverage tool in order to get branch coverage to display?

My test project uses xUnit. Could that be the cause of the 0% coverage?

Hey @p97

SonarCloud only consumes test/coverage reports, it does not produce them. I would suggest focusing on getting your reports produced correctly before worrying about importing them elsewhere.

You might also find some inspiration here:

Thank you.