Skip to end of metadata
Go to start of metadata

.NET Code Coverage is imported into SonarQube in 4 steps:

  1. Run the SonarScanner.MSBuild.exe begin command, specifying the absolute path where the code coverage report will be available using the /d:propertyKey=path syntax ("propertyKey" depends on the code coverage tool)
  2. Build your project using MSBuild
  3. Run your code coverage tool, instructing it to produce a report at the same location specified earlier to the SonarQube MSBuild Scanner
  4. Run the SonarScanner.MSBuild.exe end command

Importing multiple reports

You can import reports from different tools by passing several /d:propertyKey="path" arguments (with different property keys) to the SonarScanner.MSBuild.exe begin command.

Multiple reports from the tool are supported by:

  • Either separating paths with commas: /d:propertyKey="path1,path2,path3"
  • Or using wildcards (*, **, ?): /d:propertyKey="path/*.xml,/path/**/*.xml,/path/testreports/testreport?.xml"

Visual Basic .NET support

Examples on this page are for C#. VB.NET Plugin users should replace ".cs." by ".vbnet." in all property name. Example: "sonar.cs.vscoveragexml.reportsPaths" becomes "sonar.vbnet.vscoveragexml.reportsPaths"

Import of Integration Tests Code Coverage

SINCE C# 5.1

Integration test code coverage is supported by prefixing ".reportsPaths" in property names with ".it". Example: "sonar.cs.vscoveragexml.reportsPaths" becomes "sonar.cs.vscoveragexml.it.reportsPaths".

Here are the exact steps to import Unit Tests Code Coverage for each of the supported tool, using the SonarQube Scanner for MSBuild from the command line:

Visual Studio Code Coverage

  1. Open a Developer Command Prompt for Visual Studio
  2. Put yourself in the root folder of the project you want to analyze
  3. Run the following commands:
Begin the SonarQube Analysis and provide all required properties, including "sonar.cs.vscoveragexml.reportsPaths"
SonarScanner.MSBuild.exe begin /k:"sonarqube_project_key" /n:"sonarqube_project_name" /v:"sonarqube_project_version" /d:sonar.cs.vscoveragexml.reportsPaths="%CD%\VisualStudio.coveragexml"
Build the project, for example:
msbuild
Run Unit Tests & Collect Code Coverage
"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" collect /output:"%CD%\VisualStudio.coverage" "%VSINSTALLDIR%\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "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"
End the SonarQube Analysis and upload it to the SonarQube server
SonarScanner.MSBuild.exe end

.Net Core

For projects using the roslyn based project system, vstest coverage requires <DebugType>Full</DebugType> to be set on both the unit test projects and the production projects. For more information, please refer to https://github.com/Microsoft/vstest/issues/800

dotCover

  1. Open a Developer Command Prompt for Visual Studio
  2. Put yourself in the root folder of the project you want to analyze
  3. Run the following commands:
Begin the SonarQube Analysis and provide all required properties, including "sonar.cs.dotcover.reportsPaths"
SonarScanner.MSBuild.exe begin /k:"sonarqube_project_key" /n:"sonarqube_project_name" /v:"sonarqube_project_version" /d:sonar.cs.dotcover.reportsPaths="%CD%\dotCover.html"
Build the project, for example:
msbuild
Run Unit Tests & 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"
End the SonarQube Analysis and upload it to the SonarQube server
SonarScanner.MSBuild.exe end

OpenCover

  1. Open a Developer Command Prompt for Visual Studio
  2. Put yourself in the root folder of the project you want to analyze
  3. Run the following commands:
Begin the SonarQube Analysis and provide all required properties, including "sonar.cs.opencover.reportsPaths"
SonarScanner.MSBuild.exe begin /k:"sonarqube_project_key" /n:"sonarqube_project_name" /v:"sonarqube_project_version" /d:sonar.cs.opencover.reportsPaths="%CD%\opencover.xml"
Build the project, for example:
msbuild
Run Unit Tests & 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"

End the SonarQube Analysis and upload it to the SonarQube server
SonarScanner.MSBuild.exe end

NCover 3 (Visual Studio 2013 and earlier only - not compatible with Visual Studio 2015)

NCover 4 and 5 are not supported

Only NCover 3 reports are currently supported. While converting NCover 4 and 5 reports into NCover 3 ones might be possible, it has not been tested.

  1. Open a Developer Command Prompt for Visual Studio 2013
  2. Put yourself in the root folder of the project you want to analyze
  3. Run the following commands:
Begin the SonarQube Analysis and provide all required properties, including "sonar.cs.ncover3.reportsPaths"
SonarScanner.MSBuild.exe begin /k:"sonarqube_project_key" /n:"sonarqube_project_name" /v:"sonarqube_project_version" /d:sonar.cs.ncover3.reportsPaths="%CD%\coverage.nccov"
Build the project, for example:
msbuild
Run Unit Tests & 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"
End the SonarQube Analysis and upload it to the SonarQube server
SonarScanner.MSBuild.exe end
  • No labels