[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] 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 is showing as 0.0% while i can see the code bugs, vulnerabillity and code smell issues
Code coverage always 0%
Sonarqube Unit test and integration testing code coverage .net core
UnitTest number always 0
Unit test coverage fail when other information(duplications, code smell) is working fine
SonarQube compatibility with UFT?
SonarQube C# Code Coverage include Unit Test Project
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
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
How do I get SonarCloud to report on nUnit test coverage rather than MS Test
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
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
Unit Test Coverage Metrics on Conditions, branches and Others
Sonarqube showing 0% coverage
"WARN: Unable to get next XML event while parsing file" .NET coverage problem
dotCover coverage data not showing in SonarCloud
SonarQube 7.9: Set Code Coverage Parameter in Build Phase of Jenkins
.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
Scanner for MSBuild URL https://docs.sonarqube.org/x/CoB is broken
Not able to generate code coverage with opencover for .NET application
SonarQube - capturing unit test coverage
Sonar scanner does not scan my .VB files
CodeCoverage not found when installed by Visual Studio Test Platform Installer
Unable to see code coverage in Integration Test API Endpoint using TestHost and related type
Code Coverage Results Not Posting
SonarQube Code Coverage is not publishing from azure devops pipeline after supplied codecoverage.settings file
SonarQube - SpecFlow
Coverage tests in Sonarqube 8.1
Could not find any coverage report file matching the pattern
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!