Code Coverage not reportet to SonarQube

Hello,

we have some problems with code coverage. Our Projet has an XUnit Test Project which will also run in the pipeline. The code coverage is recognized in the azure pipeline and reported there. But not on our SonarQube server. There is always 0.0%.

Screenshot 2024-02-28 195823

Pipeline based on the example in the SonarQube Docs: .NET test coverage (sonarsource.com) and lools like this:

steps:
- task: SonarQubePrepare@4
  inputs:
    SonarQube: '***'
    scannerMode: 'MSBuild'
    projectKey: '***'
    projectName: '***'
      extraProperties: |
      sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.cobertura.xml

- task: DotNetCoreCLI@2
  displayName: 'Restore Packages'
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'select'
    vstsFeed: '***'

- task: DotNetCoreCLI@2
  displayName: 'Build Project'
  inputs:
    projects: '**/*.csproj'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    arguments: '--collect:"XPlat Code Coverage"'

- task: SonarQubeAnalyze@4

- task: SonarQubePublish@4
  inputs:
    pollingTimeoutSec: '300'

Can anybode help us?

Problen and solution found. Within the logs of the analyser I found following warning:

2024-02-28T19:31:31.9309938Z INFO: Parsing the OpenCover report /home/vsts/work/_temp/623816fd-d37e-47e5-a1c5-e401327bc55f/coverage.cobertura.xml
2024-02-28T19:31:31.9310709Z WARN: Could not import coverage report '/home/vsts/work/_temp/623816fd-d37e-47e5-a1c5-e401327bc55f/coverage.cobertura.xml' because 'Missing root element <CoverageSession> in /home/vsts/work/_temp/623816fd-d37e-47e5-a1c5-e401327bc55f/coverage.cobertura.xml at line 2'.

Due to this error, I changed the format from Cobertura to OpenCover

 -task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    arguments: '--collect "XPlat code coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover'

And also do not forget to adjust the path to the report file in prepare Task:

- task: SonarQubePrepare@4
  inputs:
     ...
    extraProperties: |
      sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml``

And now, we can see the coverage in SonarQube!

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