[Coverage & Test Data] Generate Reports for C#, VB.net

To include test coverage in your analysis, you’ll need to:

We support:

  • Import of coverage reports from VSTest, dotCover, OpenCover, Coverlet and NCover 3.
  • Import of unit test results from VSTest, NUnit and xUnit.

We’ve documented here for each engine:

  • what is the relevant analysis parameter to pass during the begin step.
  • how to generate the report.

In case you encounter problems along the way, check the Troubleshooting guide for .NET code coverage import.

Import test coverage reports

To import a test coverage report, during the Begin step you need to pass a parameter that points to the file that will be generated:

  • for C#
    • sonar.cs.vscoveragexml.reportsPaths for Visual Studio Code Coverage
    • sonar.cs.dotcover.reportsPaths for dotCover
    • sonar.cs.opencover.reportsPaths for OpenCover or Coverlet
    • sonar.cs.ncover3.reportsPaths for NCover3 (deprecated)
  • for VB .NET
    • sonar.vbnet.vscoveragexml.reportsPaths for Visual Studio Code Coverage
    • sonar.vbnet.dotcover.reportsPaths for dotCover
    • sonar.vbnet.opencover.reportsPaths for OpenCover or Coverlet
    • sonar.vbnet.ncover3.reportsPaths for NCover3 (deprecated)

You can find more details about this in Test Coverage & Execution.

Visual Studio Code Coverage

Run Unit Tests To Collect Code Coverage

"%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /EnableCodeCoverage "UnitTestProject1\bin\Debug\UnitTestProject1.dll"

Convert the Code Coverage Report from Binary into XML

"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output: "%CD%\VisualStudio.coveragexml" "%CD%\VisualStudio.coverage"

Note: If you use dotnet instead of vstest, the commands are slightly different, but the output is the same (.coverage and .xml files)
dotnet test ... --collect "DotnetCodeCoverage"
CodeCoverage.exe analyze /output: "%CD%\DotnetCoverage.coveragexml" "%CD%\DotnetCoverage.coverage"

External documentation

dotCover

Run Unit Tests To Collect Code Coverage

"%LOCALAPPDATA%\JetBrains\Installations\dotCover02\dotCover.exe" analyse /ReportType=HTML /Output="%CD%\dotCover.html" "/TargetExecutable=%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /TargetWorkingDir=. "/TargetArguments=UnitTestProject1\bin\Debug\UnitTestProject1.dll"

OpenCover

Run Unit Tests To Collect Code Coverage

"%LOCALAPPDATA%\Apps\OpenCover\OpenCover.Console.exe" -output:"%CD%\opencover.xml" -register:user -target:"%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" -targetargs:"UnitTestProject1\bin\Debug\UnitTestProject1.dll"

Coverlet

Run Unit Tests To Collect Code Coverage in OpenCover format

coverlet UnitTestProject1\bin\Debug\UnitTestProject1.dll --target "%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" --targetargs "UnitTestProject1\bin\Debug\UnitTestProject1.dll" --format opencover

NCover 3 (deprecated)

Run Unit Tests To Collect Code Coverage

"%ProgramFiles(x86)%\NCover\NCover.Console.exe" "%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "UnitTestProject1\bin\Debug\UnitTestProject1.dll" //x "%CD%\coverage.nccov"

Import unit test results

To import a test execution report, during the Begin step you need to pass a parameter that points to the file that will be generated:

  • for C#:
    • sonar.cs.vstest.reportsPaths for VSTest
    • sonar.cs.nunit.reportsPaths for NUnit
    • sonar.cs.xunit.reportsPaths for xUnit
  • for VB .NET
    • sonar.vbnet.vstest.reportsPaths for VSTest
    • sonar.vbnet.nunit.reportsPaths for NUnit
    • sonar.vbnet.xunit.reportsPaths for xUnit

You can find more details about this in Test Coverage & Execution.

VSTest

Run Unit Tests and Save Results in the “TestResults” folder using a generated *.trx filename

"%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" /Logger:trx "UnitTestProject1\bin\Debug\UnitTestProject1.dll"

NUnit

Run Unit Tests and Save Results in file "NUnitResults.xml"

packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console.exe --result=NUnitResults.xml "NUnitTestProject1\bin\Debug\NUnitTestProject1.dll"

or, for older NUnit 2

"%ProgramFiles(x86)%\NUnit 2.6.4\bin\nunit-console.exe /result=NUnitResults.xml "NUnitTestProject1\bin\Debug\NUnitTestProject1.dll"

xUnit

Run Unit Tests and Save Results in file "XUnitResults.xml"

packages\xunit.runner.console.2.1.0\tools\xunit.console.exe XUnitProject1\bin\Debug\XUnitProject1.dll -xml XUnitResults.xml

9 Likes
Coverage not reported to SonarQube
[Coverage] Troubleshooting guide for .NET code coverage import
Sonarqube C# unit test results
.NET & C# projects code coverage is not showing
Coverage does not show from CI tool but works from local machine
Sonarqube analysis success,Quality gte failed
Code coverage is not showing for .net framework C#
Code coverage is showing as 0.0% while i can see the code bugs, vulnerabillity and code smell issues
Code coverage always 0%
UnitTest number always 0
SonarQube compatibility with UFT?
Sonarqube Unit test and integration testing code coverage .net core
Unit test coverage fail when other information(duplications, code smell) is working fine
Could not import xunit test report
How to run coverage for the analysis in .net and java?
Code Coverage is not shown in the SonarCloud.io
SonarQube C# Code Coverage include Unit Test Project
SonarQube 7.9: Set Code Coverage Parameter in Build Phase of Jenkins
Scanner for MSBuild URL https://docs.sonarqube.org/x/CoB is broken
Unit Test Coverage Metrics on Conditions, branches and Others
How do I get SonarCloud to report on nUnit test coverage rather than MS Test
.NET C# solution with some integration tests, but no unit tests. Can't generate code coverage…
Configure SonarQube with .net project on Jenkins
SonarC# support for code to unit test linking
CodeCoverage not found when installed by Visual Studio Test Platform Installer
Is it possible code coverage in sonarqube for .netc#
Sonar Cloud - Quality Gate failure
C# Exception Code Coverage in Azure DevOps
How to integrate MSTEST from Jenkins to Sonarqube
Unable to see code coverage in Integration Test API Endpoint using TestHost and related type
SonarQube - SpecFlow
Need help for Unit Test Code coverage for SonarCloud
Code coverage import problem
SonarQube does not show the Code Coverage but shows the number of unit tests
Completely lost trying to collect Code Coverage
Importing coverage from .coveragexml file
Failed status of project
Sonarscanner converts codecoverage to xml then appears to not use it
SonarQube scanner not retrieving dotnet xUnit testing code coverage
Coverage tests in Sonarqube 8.1
Could not find any coverage report file matching the pattern
Publish coverage reports post msbuild in Jenkins job
SonarQube doesn't show Code Coverage for some projects
Code coverage is showing as 0.0% while i can see the code bugs, vulnerabillity ,code smell issues, duplications etc
VB.Net - SonarQube Community Edition v8.0 - no code coverage stats and encoding issues
How to add test coverage task using Azure DevOps build pipeline
Azure CI pipeline how to configure code coverage . Currently it was zero percentage on the code coverage

3 posts were split to a new topic: Problem with importing code coverage for a .NET project

A post was split to a new topic: Question about nunit3 command

For any question around coverage and test data, please open a topic in the Get Help category. Thank you!