[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, Altcover 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, Coverlet or Altcover
    • 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, Coverlet or Altcover
    • 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


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"


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"


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


Altcover by default is using the OpenCover format, that is already supported.

Add the package to your test project
dotnet add package AltCover

Run tests with coverage
dotnet test /p:AltCover=true

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.


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"


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"


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

[Coverage] Troubleshooting guide for .NET code coverage import
Coverage not reported to SonarQube
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 always 0%
Code coverage is showing as 0.0% while i can see the code bugs, vulnerabillity and code smell issues
SonarQube compatibility with UFT?
Unit test coverage fail when other information(duplications, code smell) is working fine
Sonarqube Unit test and integration testing code coverage .net core
UnitTest number always 0
How to run coverage for the analysis in .net and java?
Could not import xunit test report
Code Coverage is not shown in the SonarCloud.io
SonarQube C# Code Coverage include Unit Test Project
Importing coverage from .coveragexml file
Failed status of project
Code coverage not shown for .NET 6 projects
Sonarscanner converts codecoverage to xml then appears to not use it
Get failed message when end scan my .net framework project
SonarQube scanner not retrieving dotnet xUnit testing code coverage
How can I publish multiple code coverage report in a single build on Azure DevOps
Publish coverage reports post msbuild in Jenkins job
SonarQube doesn't show Code Coverage for some projects
Sonarqube showing 0% coverage
"WARN: Unable to get next XML event while parsing file" .NET coverage problem
dotCover coverage data not showing in SonarCloud
.NET C# solution with some integration tests, but no unit tests. Can't generate code coverage…
Code Smells only getting generated for Dot Net 2015 and framework 4.0 windows application
SonarQube: No analysis found in this build after applying include pattern
Sonar Scanner shows 0 bugs, 0 Vulnerabilties for .net project
Xunit CodeCoverage in SonarCloud
Code coverage is 0%
Lines of code = 0 in duplication / coverage graphs or main dashboard summary LOC counters
Windows Presentation Foundation (WPF) c# code not showing code coverage in SQ dashboard though unit tests success
Where is the documentation to setup Code Coverage with Azure Devops (.net)
Distinction between Lines and Lines of Code in C# projects
SonarQube and code coverage
Not able to generate code coverage with opencover for .NET application
SonarQube - capturing unit test coverage
Sonar scanner does not scan my .VB files
Code Coverage Results Not Posting
SonarQube Code Coverage is not publishing from azure devops pipeline after supplied codecoverage.settings file
Azure CI pipeline how to configure code coverage . Currently it was zero percentage on the code coverage
How to add test coverage task using Azure DevOps build pipeline
VB.Net - SonarQube Community Edition v8.0 - no code coverage stats and encoding issues
Code coverage is showing as 0.0% while i can see the code bugs, vulnerabillity ,code smell issues, duplications etc
Could not find any coverage report file matching the pattern
Coverage tests in Sonarqube 8.1
SonarQube - SpecFlow
Unable to see code coverage in Integration Test API Endpoint using TestHost and related type
CodeCoverage not found when installed by Visual Studio Test Platform Installer
Scanner for MSBuild URL https://docs.sonarqube.org/x/CoB is broken
SonarQube 7.9: Set Code Coverage Parameter in Build Phase of Jenkins
Unit Test Coverage Metrics on Conditions, branches and Others
How do I get SonarCloud to report on nUnit test coverage rather than MS Test
Configure SonarQube with .net project on Jenkins
SonarC# support for code to unit test linking
How do i import code coverage in my github action
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
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

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!