Need help for Unit Test Code coverage for SonarCloud

Hi Team,
I am working on dotnet project and I was able to successfully generate sonar cloud scan analysis report from Azure DevOps by using the Sonar Cloud Pugin. However, the Coverage and Unit Tests section shows 0.0%. Could you help me as to what would be required to include the unit test coverage results?

Welcome, @palak.joshi!

How are you triggering your unit test and code coverage files? You need to make sure of the following:

  1. Are you using VSTest, NUnit, or xUnit for unit tests? If so, make sure you create the *.trx or *.xml file and set its path for the associated sonar property. See “Import unit test results” section in [Coverage & Test Data] Generate Reports for C#,
  2. Are you using Visual Studio CodeCoverage, dotCover, OpenCover, or Coverlet? If so, make sure to generate the code coverage file first then set its path to the associated sonar property. See “Import test coverage reports” in [Coverage & Test Data] Generate Reports for C#,
  3. Make sure to add these 2 sonar properties (one for unit tests, one for code coverage) to the MSBuild begin step. Then you can proceed with the build step and the end step.

Here is where I got the instructions:

Here is an example I ran with VSTest and Visual Studio Code Coverage:

# run unit tests and save results
$ vstest.console.exe /Logger:trx "SomeConsoleApplicationTest\bin\Debug\SomeConsoleApplicationTest.dll"

# generate code coverage
$ vstest.console.exe --EnableCodeCoverage "SomeConsoleApplicationTest\bin\Debug\SomeConsoleApplicationTest.dll"

# covert the binary .coverage file into .coveragexml
$ CodeCoverage.exe analyze /output:"C:\Users\jojo\Projects\sonar-scanning-examples\sonarqube-scanner-msbuild\CSharpProject\TestResults\dee3f166-f02a-432c-8b4d-3abca4467bb5\jojo_joeTingsanchali 2020-10-14 19_02_26.coveragexml" "C:\Users\jojo\Projects\sonar-scanning-examples\sonarqube-scanner-msbuild\CSharpProject\TestResults\dee3f166-f02a-432c-8b4d-3abca4467bb5\jojo_joeTingsanchali 2020-10-14 19_02_26.coverage"

# Begin step with the vstest (for unit tests) and vscoveragexml (for code coverage) parameters
$ SonarScanner.MSBuild.exe begin -k:"CSharpProjectExample" -d:"" -d:"sonar.cs.vstest.reportsPaths=**/*.trx" -d:"sonar.verbose=true"-d:"sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml"

# Rebuild
$ MSBuild.exe ./SomeConsoleApplication.sln -t:Rebuild

# End
$ SonarScanner.MSBuild.exe end

If this is confusing or these instructions don’t work, please provide the commands you used and the scanner log output from your commands including using -d:"sonar.verbose=true" (or /d) in the MSBuild begin step.


1 Like

Hi @Joe_Tingsanchali , thanks for getting back. By the begin step, do you mean “Prepare Analysis on SonarCloud step”?

I added the sonar properties as:


and Please find below the Agent information below for running the pipeline:

Please find below the log for the Run Code Analysis step.SonarCloud Log codecoverage.txt (5.9 KB)

Could you please help me if I’m missing something?