SonarCloud incorrectly showing 0% Code Coverage

  • ALM used : Azure DevOps
  • CI system used : Azure DevOps
  • Scanner command used :
 - task: SonarCloudPrepare@1
            inputs:
              SonarCloud: '[name of the service connection'
              organization: '[name of our organization]'
              scannerMode: 'MSBuild'
              projectKey: '[project key for our project]'
              projectName: '[name of our project]'
              extraProperties: |
                  sonar.cs.vstest.reportsPaths=$(TestOutputDirectory)/*.trx
                  sonar.coverageReportPaths=$(TestOutputDirectory)/mergedcoveragereport/SonarQube.xml
                  sonar.coverage.exclusions=**/*Tests*/**/*
                  sonar.inclusions=**/*.cs

          - task: DotNetCoreCLI@2
            displayName: 'NuGet Restore'
            inputs:
              command: 'restore'
              feedsToUse: 'config'
              nugetConfigPath: 'nuget.config'

          - task: DotNetCoreCLI@2
            displayName: 'Build solution'
            inputs:
              command: 'build'

          - task: DotNetCoreCLI@2
            displayName: 'Test solution'
            continueOnError: true
            inputs:
              command: 'test'
              projects: '**/*Tests.csproj'
              arguments: '--logger trx --collect:"XPlat Code Coverage" --results-directory $(TestOutputDirectory)'
              publishTestResults: false

          - task: PublishTestResults@2
            displayName: 'Publish Test Results'
            inputs:
              testResultsFormat: 'VSTest'
              testResultsFiles: '*.trx'
              searchFolder: '$(TestOutputDirectory)'
              failTaskOnFailedTests: true

          - task: reportgenerator@5
            inputs:
              reports: '$(TestOutputDirectory)/*/coverage.cobertura.xml'
              targetdir: '$(TestOutputDirectory)/mergedcoveragereport'
              reporttypes: 'Cobertura;SonarQube'
              assemblyfilters: '-*Tests*'

          - task: PublishCodeCoverageResults@1
            displayName: 'Publish CodeCoverage Results'
            inputs:
              codeCoverageTool: 'Cobertura'
              summaryFileLocation: '$(TestOutputDirectory)/mergedcoveragereport/Cobertura.xml'
              failIfCoverageEmpty: true

          - task: SonarCloudAnalyze@1
            displayName: 'SonarCloud Analyze'

          - task: SonarCloudPublish@1
            displayName: 'SonarCloud Publish'
            inputs:
              pollingTimeoutSec: '300'
  • Languages of the repository : C#

  • Error observed: SonarCloud incorrectly showing 0% code coverage, although 155 out of 202 lines are actually covered according to SonarCloud:
    image
    SonarCloud Analysis log.txt (4.9 KB)
    Cobertura.txt (28.3 KB)

What can we do to fix the 0% code coverage being shown?

Hello @JorijnLamberink ,

Thank you for reporting this. I will try to investigate, my first ideas will be to check the configuration and availability of the coverage report to the scanner.

Hi Florian,

We noticed the lines of code dropped significantly from a few hundred to only 3 :
image

So it seems somehow the coverage report is not coming over in the right way or something, but we have no clue. Since another project we have, is set up in exactly the same way, but we have lines of code and a coverage on that project.

Greeting,
Jorijn

Hello @JorijnLamberink

Sorry for the delay.

I see attached in your previous message a file Cobertura.txt

In parallel I see in the SC Config the following coverage report used.

$(TestOutputDirectory)/mergedcoveragereport/SonarQube.xml

Can you please send me privately this coverage report named SonarQube.xml, so I can have a look?

Hi Florian,

Sorry for my delayed reaction, but here is the SonarQube.xml you wanted to see.
(since the forum does not allow xml files I had to zip it)
SonarQube.zip (1.1 KB)

Hi Florian,

Apparently the project was referencing some testing libraries mentioned in Analysis of product projects vs. test projects.

After removing them and adding the following to the .csproj

<SonarQubeTestProject>false</SonarQubeTestProject>

It is still not showing any code coverage.

Hi Florian,
I also encountered this issue, most of the c# code are not recognized by sonar cloud suddenly. However some are recognized.

1 Like

removing the references to the testing libraries and adding the
<SonarQubeTestProject>false</SonarQubeTestProject>
did solve it after all

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.