Unit Test Coverage Set Up

When I run the sonar analysis on PR’s I see the following message in the coverage section of the Sonar report:
A few extra steps are needed for SonarCloud to analyze your code coverage
Setup coverage analysis

I think that I have completed all of the steps in the link. What am I missing?

Here is my ymal file:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
# Charles Teague November 22, 2023

trigger:
- main
- QA

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- checkout: self
  fetchDepth: 0
- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'SonarCloud LayoutBoards'
    organization: '[Redacted]'
    scannerMode: 'MSBuild'
    projectKey: '[Redacted]'
    projectName: '[Redacted]'
    extraProperties: |
      sonar.cs.xunit.reportsPaths=${Agent.TempDirectory}/**/XUnit.TestResults.xml 
      sonar.cs.opencover.reportsPaths=${Agent.TempDirectory}/**/opencoverCoverage.xml
    
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: Test
  inputs:
    command: test
    projects: ‘**/*Tests.csproj’
    arguments: '--configuration $(BuildConfiguration) --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=$(Agent.TempDirectory)/coverage/ /p:MergeWith=$(Agent.TempDirectory)/coverage/ --collect "Code coverage"'

- task: PublishTestResults@2
  displayName: "Publish Test Results"
  inputs:
    testResultsFormat: VSTest
    testResultsFiles: "$(Agent.TempDirectory)/**.*trx"
    mergeTestResults: true
    condition: succeededOrFailed()
- task: SonarCloudAnalyze@1
  inputs:
    jdkversion: 'JAVA_HOME_17_X64'
- task: SonarCloudPublish@1
  inputs:
    pollingTimeoutSec: '300'

Template for a good new topic, formatted with Markdown:

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

When I change code that I have covered with a test, it shows 0 lines covered.

The above will not work without Visual Studio Enterprise addition. So, I switch to using Opencover and it worked great. I followed .NET Core Code Coverage done right – writeabout.net and modified its yml to work on my solution. This allow me to get coverage reports in Both Azure Dev opts and Sonar. Here is my yml:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
# Charles Teague November 22, 2023

trigger:
- main
- QA

pool:
  vmImage: 'windows-latest'

variables:
- name: system.debug
  value: true 
- name: solution
  value: '**/*.sln'
- name: buildPlatform
  value: 'Any CPU'
- name: buildConfiguration
  value: 'Release'


steps:
- checkout: self
  fetchDepth: 0
- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '$(solution)'
    feedsToUse: 'select'

- task: SonarCloudPrepare@1
  inputs:
    SonarCloud: 'SonarCloud LayoutBoards'
    organization: '[redacted]'
    scannerMode: 'MSBuild'
    projectKey: '[redacted]'
    projectName: 'layoutboards'
    extraProperties: |
      sonar.cs.xunit.reportsPaths=${Agent.TempDirectory}/**/XUnit.TestResults.xml 
      sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/coverage/coverage.opencover.xml

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '$(solution)'
 
- script: |
    dotnet test --logger trx --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=$(Build.SourcesDirectory)/coverage/ $(Build.SourcesDirectory)\xUnitLayout\xUnitLayoutTests.csproj
  displayName: 'dotnet test'

- script: |
    dotnet tool install dotnet-reportgenerator-globaltool --tool-path . 
    ./reportgenerator "-reports:$(Build.SourcesDirectory)/coverage/coverage.opencover.xml" "-targetdir:coverage/Cobertura" "-reporttypes:Cobertura;HTMLInline;HTMLChart"
  condition: eq( variables['Agent.OS'], 'Linux' )
  displayName: Run Reportgenerator on Linux
 
- script: |
    dotnet tool install dotnet-reportgenerator-globaltool --tool-path .
    .\reportgenerator.exe "-reports:$(Build.SourcesDirectory)/coverage/coverage.opencover.xml" "-targetdir:coverage/Cobertura" "-reporttypes:Cobertura;HTMLInline;HTMLChart"
  condition: eq( variables['Agent.OS'], 'Windows_NT' )
  displayName: Run Reportgenerator on Windows

- task: SonarSource.sonarcloud.ce096e50-6155-4de8-8800-4221aaeed4a1.SonarCloudAnalyze@1
  displayName: 'Run Code Analysis'
 
- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'
     
- task: PublishCodeCoverageResults@1
  inputs:
    summaryFileLocation: $(Build.SourcesDirectory)/coverage/Cobertura/Cobertura.xml
    reportDirectory: $(Build.SourcesDirectory)/coverage/Cobertura
    codecoverageTool: cobertura


- task: SonarCloudPublish@1
  inputs:
    pollingTimeoutSec: '300'


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