# Unit Test Coverage Set Up

**URL:** <https://community.sonarsource.com/t/unit-test-coverage-set-up/106757>\
**Category:** SonarQube Cloud\
**Tags:** yml, dotnet, sonarqube-cloud, coverage\
**Created:** [January 3, 2024, 7:19pm UTC](https://community.sonarsource.com/t/unit-test-coverage-set-up/106757 "2024-01-03T19:19:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![csteague7](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/csteague7/32/52659_2.png) [@csteague7](https://community.sonarsource.com/u/csteague7)\
**Post date:** [January 3, 2024, 7:19pm UTC](https://community.sonarsource.com/t/unit-test-coverage-set-up/106757/1 "2024-01-03T19:19:24Z")

</div>

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](https://docs.sonarsource.com/sonarcloud/enriching/test-coverage/overview/)

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

Here is my ymal file:

```auto
# 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](https://commonmark.org/help/):

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

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/2/e/2e047ef4f4a1e82c42564aebbd306a9ac7a72398.png)

---

<div class="post-metadata">

**Author:** ![csteague7](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/csteague7/32/52659_2.png) [@csteague7](https://community.sonarsource.com/u/csteague7)\
**Post date:** [January 4, 2024, 2:43pm UTC](https://community.sonarsource.com/t/unit-test-coverage-set-up/106757/3 "2024-01-04T14:43:11Z")

</div>

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

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/0/9/09a9a95180cbdd5d1584767e93af0bc660e954df.png)

---

<div class="post-metadata">

**Author:** ![csteague7](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/csteague7/32/52659_2.png) [@csteague7](https://community.sonarsource.com/u/csteague7)\
**Post date:** [January 4, 2024, 7:57pm UTC](https://community.sonarsource.com/t/unit-test-coverage-set-up/106757/4 "2024-01-04T19:57:43Z")

</div>

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](https://writeabout.net/2019/04/27/net-core-code-coverage-done-right/) 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:

```auto
# 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'

```

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/9/e/9e2d90755bc8b9db6c5e03d06f793899e0c06582.png)  
 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/7/3/739203c837eb0952508d87ea006a9ee6f3aa1d74.png)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [January 11, 2024, 7:58pm UTC](https://community.sonarsource.com/t/unit-test-coverage-set-up/106757/5 "2024-01-11T19:58:17Z")

</div>

Closed on January 11, 2024, 7:58pm UTC

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