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#, VB.net.
  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#, VB.net.
  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:"sonar.host.url=http://localhost:9000" -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.

Joe

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:

sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml
sonar.cs.vstest.reportsPaths=**/*.trx
sonar.debug=true
sonar.verbose=true

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

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?

HI, @Joe awaiting your response on the above commands and what extra I need to add.

Hello @palak.joshi,

I apologize for my delayed response. Thank you for the log, it looks like it found the .coverage file and converted it correctly into the .coveragexml file. Can you confirm there is something inside both files (i.e. make sure it’s not blank or empty)? Can you show the logs for the entire build (MSBuild.exe begin, rebuild. MSBuild.exe end steps, etc.)? It’s very strange that no other logs are shown in your attachment for the SonarCloud step.

Here’s an example for you using VSTest and VSCoverageXML that worked for me:

  • Note the extraProperties I have listed for the SonarCloudPrepare@1 (“Prepare Analysis for SonarCloud” task). Try referencing the $(Agent.TempDirectory), although it looks like your log show its defaulted to the correct base directory already.
trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: VisualStudioTestPlatformInstaller@1
  inputs:
    packageFeedSelector: 'nugetOrg'
    versionSelector: 'latestPreRelease'
- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'
- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'sonarcloud'
    organization: 'joetingsanchali'
    scannerMode: 'MSBuild'
    projectKey: 'joetingsanchali_sonar-scanning-someconsoleapp'
    projectName: 'sonar-scanning-someconsoleapp'
    extraProperties: |
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)\**\*.trx
      sonar.cs.vscoveragexml.reportsPaths=$(Agent.TempDirectory)\**\*.coveragexml
      sonar.verbose=true
- task: MSBuild@1
  inputs:
    solution: '**\*.sln'
    msbuildArguments: '-t:Rebuild'
- task: VSTest@2
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test*.dll
      !**\*TestAdapter.dll
      !**\obj\**
    codeCoverageEnabled: true
    searchFolder: '$(System.DefaultWorkingDirectory)'
- task: SonarCloudAnalyze@1
- task: SonarCloudPublish@1
  inputs:
    pollingTimeoutSec: '300'

By the way, you can remove sonar.debug=true since that doesn’t do anything.

Joe