Code Coverage 0% in SonarCloud

  • ALM used: Azure DevOps
  • CI system used: Azure DevOps
  • Languages of the repository: c#

The code coverage is correctly displayed in Azure Devops, but remains empty in SonarCloud. It worked before, but stopped working a couple of weeks ago without any apparent reason.

yaml pipeline:

parameters:
  - name: solutionPath
  - name: nugetConfigPath
  - name: buildConfiguration
    default: 'Release'
  - name: sonarProjectKey
  - name: sonarProjectName 
  - name: azureSubscription

steps:
  - checkout: self
    fetchDepth: 0

  - task: NuGetAuthenticate@1

  - task : NuGetToolInstaller@1

  - task: SonarCloudPrepare@1
    inputs:
      scannerMode: "MSBuild"
      projectKey: ${{ parameters.sonarProjectKey }}
      projectName: ${{ parameters.sonarProjectName }}
      SonarCloud: "mijncontoso-sonarcloud"
      organization: "contoso-1"
      # the coverage.opencover.xml files are generated using the 'DotNet test' task by specifiying the opencover format in the --collect parameter
      extraProperties: |
        sonar.scanner.metadataFilePath=$(Agent.TempDirectory)/sonar/$(Build.BuildNumber)/test/report-task.txt
        sonar.exclusions=**/obj/**,**/*.dll
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
        sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml

  - task: DotNetCoreCLI@2
    inputs:
      command: 'restore'
      projects: '${{ parameters.solutionPath }}'
      feedsToUse: 'config'
      nugetConfigPath: '${{ parameters.nugetConfigPath }}'

  - task: DotNetCoreCLI@2
    displayName: DotNet build
    inputs:
      command: build
      projects: ${{ parameters.solutionPath }}
      arguments: -c ${{ parameters.buildConfiguration }} --no-restore

  - task: AzureCLI@2
    displayName: Expose service principal credentials to pipeline
    inputs:
      azureSubscription: ${{ parameters.azureSubscription }}
      scriptType: pscore
      scriptPath: pipelines/expose-service-principal-creds.ps1
      addSpnToEnvironment: true
  
  - task: DotNetCoreCLI@2
    displayName: DotNet test
    inputs:
      command: test
      projects: |
        **/*Test.csproj
        **/*Tests.csproj
      nobuild: true
      # The --collect parameter is used to collect code coverage data.
      # Supported coverage data formats are: cobertura, json, lcov, opencover, teamcity and are documented here: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
      # The opencover format is used by SonarCloud
      # The cobertura format is used by Azure Devops, the PublishCodeCoverageResults task is used to make the cobertura files available to Azure Devops
      # The filter parameter is used to exclude tests that are marked with the LocalOnly or UsesTestContainer attributes.
      arguments: --configuration ${{ parameters.buildConfiguration }} --no-restore --collect:"XPlat Code Coverage;Format=cobertura,opencover" --filter TestCategory!=LocalOnly&TestCategory!=UsesTestContainer
      # publishTestResults: true casues the --logger parameter to be set to 'trx' and the --results-directory set to $(Agent.TempDirectory). 
      # Set publishTestResults to 'false' if you want to explicitly set the --logger and/or --results-directory parameters yourself.
      publishTestResults: true 
    env:
      AZURE_TENANT_ID: 12345678-1234-1234-1234-1234567890123
      AZURE_CLIENT_ID: $(servicePrincipalId)
      AZURE_CLIENT_SECRET: $(servicePrincipalKey)

  - task: PublishCodeCoverageResults@2
    displayName: 'Publish code coverage report to Azure Devops'
    inputs:
      #codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
      failIfCoverageEmpty : true    

  - task: SonarCloudAnalyze@1
    #inputs:
    #  jdkversion: "JAVA_HOME_17_X64"

  - task: SonarCloudPublish@1
    #inputs:
    #  pollingTimeoutSec: "300"

  - task: sonarcloud-buildbreaker@2
    displayName: Ensure quality gate
    inputs:
      SonarCloud: mijncontoso-sonarcloud
      organization: contoso-1

No errors observed, the log indicates that code coverage files are picked up correctly, but in SonarCloud it remains 0%:

2024-05-15T07:14:52.1927159Z INFO: Parsing the OpenCover report D:\a\_temp\c5abf226-723a-4705-ae69-2a30be7a62ec\coverage.opencover.xml
2024-05-15T07:14:52.6040851Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\c5abf226-723a-4705-ae69-2a30be7a62ec\coverage.opencover.xml
2024-05-15T07:14:52.7298223Z INFO: Parsing the OpenCover report D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_12_00\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:52.8242019Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_12_00\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:53.0190234Z INFO: Parsing the OpenCover report D:\a\_temp\d2885ce4-dde9-473b-8b4e-21267904541e\coverage.opencover.xml
2024-05-15T07:14:53.2188861Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\d2885ce4-dde9-473b-8b4e-21267904541e\coverage.opencover.xml
2024-05-15T07:14:53.4158650Z INFO: Parsing the OpenCover report D:\a\_temp\fc1b2530-1cf9-475d-b236-2065a5ef9dd8\coverage.opencover.xml
2024-05-15T07:14:53.6318034Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\fc1b2530-1cf9-475d-b236-2065a5ef9dd8\coverage.opencover.xml
2024-05-15T07:14:53.8244402Z INFO: Parsing the OpenCover report D:\a\_temp\dcf992c4-c9da-448f-b57e-4575ea5a13bd\coverage.opencover.xml
2024-05-15T07:14:53.8582563Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\dcf992c4-c9da-448f-b57e-4575ea5a13bd\coverage.opencover.xml
2024-05-15T07:14:54.0169685Z INFO: Parsing the OpenCover report D:\a\_temp\20b9209b-54b0-44cc-b946-89ac02ea0ff2\coverage.opencover.xml
2024-05-15T07:14:54.2104429Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\20b9209b-54b0-44cc-b946-89ac02ea0ff2\coverage.opencover.xml
2024-05-15T07:14:54.2492752Z INFO: Parsing the OpenCover report D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_10_28\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2596803Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_10_28\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2597497Z INFO: Parsing the OpenCover report D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_12_29\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2614973Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_12_29\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2615551Z INFO: Parsing the OpenCover report D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_11_29\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2630365Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_11_29\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2630966Z INFO: Parsing the OpenCover report D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_10_55\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2631659Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_10_55\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2632180Z INFO: Parsing the OpenCover report D:\a\_temp\01972c64-8fa9-4a33-aa36-5b8834a1e4f6\coverage.opencover.xml
2024-05-15T07:14:56.2632681Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\01972c64-8fa9-4a33-aa36-5b8834a1e4f6\coverage.opencover.xml
2024-05-15T07:14:56.2633199Z INFO: Parsing the OpenCover report D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_12_56\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2633766Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_12_56\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2634314Z INFO: Parsing the OpenCover report D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_13_37\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2634882Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\VssAdministrator_fv-az390-988_2024-05-15_07_13_37\In\fv-az390-988\coverage.opencover.xml
2024-05-15T07:14:56.2635387Z INFO: Parsing the OpenCover report D:\a\_temp\49cb552e-6ec3-4390-a01a-2b4e7f4fe538\coverage.opencover.xml
2024-05-15T07:14:56.2635882Z INFO: Adding this code coverage report to the cache for later reuse: D:\a\_temp\49cb552e-6ec3-4390-a01a-2b4e7f4fe538\coverage.opencover.xml
2024-05-15T07:14:56.2636287Z INFO: Coverage Report Statistics: 329 files, 13 main files, 13 main files with coverage, 316 test files, 0 project excluded files, 0 other language files.

Hi,

Welcome to the community!

Since this was working fine, can you check the commit history of your pipeline file to see what, if anything changed before the break?

 
Thx,
Ann