Search code examples
.netazure-devops.net-framework-versionsonarcloud

Unable to publish the code coverage result into Sonar Cloud Dashboard


I have on .Net Framework Application, tried to publish the code coverage result into Sonar Cloud Dashboard, but unable to publish the code coverage result Using YAML File Task - VSTest & Codecoverage.exe

Reference YAML File

pool:    
    ${{ if ne(parameters.AgentImage, '') }}:
      vmImage: '${{parameters.AgentImage}}' 
    ${{ else }}:
        name: SelfhostedAgent
        demands:
        - vstest
        - msbuild
        - visualstudio
        - java
  
  workspace:
    clean: all

  steps:
  - checkout: self
    clean: true

  - task: NuGetToolInstaller@1
    displayName: 'Use NuGet 4.x'
    inputs:
     versionSpec: 4.x
     checkLatest: true

  - task: NuGetCommand@2
    displayName: 'Restore NuGet Packages Service'
    inputs:
      restoreSolution: '${{ parameters.serviceSolution }}'
      feedsToUse: config
      nugetConfigPath: '${{ parameters.NugetserviceSolution }}'
    condition: and(succeeded(), eq('${{ parameters.SolutionCustomPath }}', ''))

  - task: NuGetCommand@2
    displayName: 'Restore NuGet Packages Presentation'
    inputs:
      restoreSolution: '${{ parameters.PresentationSolution }}'
      feedsToUse: config
      nugetConfigPath: '${{ parameters.NugetPresentationSolution }}'
    condition: and(succeeded(), eq('${{ parameters.SolutionCustomPath }}', ''))

  - task: SonarSource.sonarcloud.14d9cde6-c1da-4d55-aa01-2965cd301255.SonarCloudPrepare@1
    displayName: 'Prepare analysis on SonarCloud'
    inputs:
      SonarCloud: SonarCloud
      organization: msc
      projectKey: ${{ parameters.SonarProjectKey }}
      projectName: ${{ parameters.SonarProjectName }}
      extraProperties: |
        sonar.exclusions= ${{ parameters.SonarExclusions }}
        sonar.cs.vstest.reportsPaths=$(Build.SourcesDirectory)\TestResult\*.trx
        sonar.cs.vscoveragexml.reportsPaths=$(Build.SourcesDirectory)\TestResult\newCodeReport.coveragexml
        sonar.verbose=true

  - task: VSBuild@1
    displayName: 'Build solution ${{ parameters.PresentationSolution }}'
    inputs:
      solution: ${{ parameters.PresentationSolution }}
      msbuildArgs: '/p:OutDir=$(Build.BinariesDirectory)/Presentation'
      platform: '$(BuildPlatform)'
      configuration: '$(BuildConfiguration)'
      clean: true
      msbuildArchitecture: x64
      logProjectEvents: false
      maximumCpuCount: true
      vsVersion: ${{ parameters.VsVersion }}

  - task: VSBuild@1
    displayName: 'Build service solution'
    inputs:
      solution: '${{ parameters.serviceSolution }}'
      msbuildArgs: '/p:OutDir=$(Build.BinariesDirectory)/Service /p:RunCodeAnalysis=false /p:CodeAnalysisTreatWarningsAsErrors=false'
      platform: '$(BuildPlatform)'
      configuration: '$(BuildConfiguration)'
      maximumCpuCount: true
      msbuildArchitecture: x64
      clean: true
      vsVersion: ${{ parameters.VsVersion }}

  - task: VSTest@2
    displayName: 'Unit Tests'
    inputs:
      testSelector: testAssemblies
      testAssemblyVer2: |
        **\Msc.*Test.dll
        !**\*TestAdapter.dll
        !**\obj\**  
      searchFolder: '$(Build.BinariesDirectory)/'
      codeCoverageEnabled: true
      resultsFolder: '$(Build.SourcesDirectory)\TestResult\'
      diagnosticsEnabled: true
      runOnlyImpactedTests: '${{ parameters.RunonlyTest}}'
      runInParallel: true
      rerunFailedTests: true
      testRunTitle: 'Unit Testing service layer'
      platform: '$(BuildPlatform)'
      configuration: '$(BuildConfiguration)'
      vsTestVersion: ${{ parameters.VsVersion }}
      
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        # Write your PowerShell commands here.
        Write-Host "Hello World"
        
        Get-ChildItem -Path "$(Build.SourcesDirectory)\TestResult\*" -Include *.coverage -Recurse | Copy-Item -Destination "$(Build.SourcesDirectory)\TestResult\" -verbose

        (Get-ChildItem -Path "$(Build.SourcesDirectory)\TestResult\" -Filter *.coverage).FullName | Rename-Item -NewName AgentName.coverage -verbose

        CodeCoverage analyze /output:$(Build.SourcesDirectory)\TestResult\newCodeReport.coveragexml $(Build.SourcesDirectory)\TestResult\AgentName.coverage
    continueOnError: true            

  - task: SonarSource.sonarcloud.ce096e50-6155-4de8-8800-4221aaeed4a1.SonarCloudAnalyze@1
    displayName: 'Run Code Analysis'
    
  - task: SonarSource.sonarcloud.38b27399-a642-40af-bb7d-9971f69712e8.SonarCloudPublish@1
    displayName: 'Publish Quality Gate Result'

I have tried to run the YAML File - Build was succeed, but code coverage results unable to publish in Sonar Cloud Dashboard

SonarCloud Dashboard

Pipeline RunCode Analysis task to Find out .coveragexml File

Code Coverage Report

Question is: If Codecovearge.exe is deprecated in SonarQube or SonarCloud. If any alternative way to publish the code coverage result into Sonar Cloud Dashboard


Solution

  • Tested from my side, the code coverage result can be published to Sonar Cloud without any issue when using VSTest task. Below is my yaml file for your reference. You can try to remove reportsPaths from extraProperties, and add scannerMode: 'MSBuild' to SonarCloudPrepare@1 task.

    - task: NuGetCommand@2
      inputs:
        command: 'restore'
        restoreSolution: '**/*.sln'
        feedsToUse: 'select'
        vstsFeed: '...'
    
    - task: SonarCloudPrepare@1
      inputs:
        SonarCloud: 'SonarCloudConsoleAppFramework'
        organization: '{SonarCloud Org name}'
        scannerMode: 'MSBuild'
        projectKey: '{projectKey}'
        projectName: '{SonarCloud project name}'
    - task: VSBuild@1
      inputs:
        solution: '**\*.sln'
        configuration: '$(buildConfiguration)'
        msbuildArchitecture: 'x64'
      
    - task: VSTest@3
      inputs:
        testSelector: 'testAssemblies'
        testAssemblyVer2: |
          **\*test*.dll
          !**\*TestAdapter.dll
          !**\obj\**
        searchFolder: '$(System.DefaultWorkingDirectory)'
        resultsFolder: '$(Agent.TempDirectory)\TestResults'
        codeCoverageEnabled: true
      
    - task: SonarCloudAnalyze@1
      inputs:
        jdkversion: 'JAVA_HOME_17_X64'
    
    - task: SonarCloudPublish@1
      inputs:
        pollingTimeoutSec: '300'
    

    The result in Sonar Cloud: enter image description here