Cannot setup coverage analysis for Azure DevOps pipeline

I am currently implementing an azure DevOps pipeline for my .Net microservices.
I researched and read the sonar cloud doc, but nothing helped yet. I did generate the code coverage report using cobertura and that is working correctly in azure devops but I found out that you cannot link that type of format to sonarcloud so I was trying somehow to generate also the opencover and use it for exporting to sonarcloud, this is what I have done so far:

trigger:
- dev
- main

pool:
  vmImage: ubuntu-latest

jobs:
- job: check_code_quality
  steps:
  - task: SonarCloudPrepare@1
    displayName: 'Setup Sonar Cloud'
    inputs:
      SonarCloud: 'SonarCloud'
      organization: '***'
      scannerMode: 'MSBuild'
      projectKey: '***'
      projectName: '****'
      extraProperties: |
        sonar.exclusions=**/obj/**,**/*.dll
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
        sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
        
  - task: DotNetCoreCLI@2  
    displayName: 'Build main code'  
    inputs:  
      command: build  
      projects: '**/*.csproj'
      arguments: '--configuration Release'
  
  - task: DotNetCoreCLI@2
    displayName: 'Run unit tests'
    continueOnError: true
    inputs:
      command: 'test'
      projects: '**/*[Tt]est*/*.csproj'
      testRunTitle: 'Backend Unit Testing'
      arguments: '--configuration Release --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover'
      publishTestResults: true
  
  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage report'
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
  
  - task: SonarCloudAnalyze@1
    displayName: 'Run Sonar Analysis'
  
  - task: SonarCloudPublish@1
    displayName: 'Publish Sonar Results'
    inputs:
      pollingTimeoutSec: '300'

I checked the logs and I saw this:

Attachments:
  /home/vsts/work/_temp/111bef07-6e19-43fe-a689-7597bc24dda3/coverage.cobertura.xml
  /home/vsts/work/_temp/111bef07-6e19-43fe-a689-7597bc24dda3/coverage.opencover.xml

Also I have an issue that sonar cloud is only analyzing my test projects not the main code

Hi,

Welcome to the community!

This is the core problem, since you can’t have coverage of tests.

Can you share your analysis logs?

The analysis / scanner log is what’s output from the analysis command. Hopefully, the log you provide - redacted as necessary - will include that command as well.

This guide will help you find them.

 
Ann

Hi @ G Ann Campbell, thank you.

Here is a part of the logs

14:22:12.994 DEBUG: The current user dir is '/home/vsts/work/1'.

14:22:12.995 INFO: Parsing the OpenCover report /home/vsts/work/_temp/_fv-az632-167_2023-01-11_14_20_13/In/fv-az632-167/coverage.opencover.xml

14:22:13.034 DEBUG: Did not find deterministic source path in '/home/vsts/work/1/s/Authorization/Class.cs'. Will skip this coverage entry. Verify sonar.sources in .sonarqube\out\sonar-project.properties.
14:22:13.036 DEBUG: CoveredFile created: (ID '1', path '/home/vsts/work/1/s/Authorization/Class.cs', NO INDEXED PATH).
14:22:15.648 DEBUG: Skipping the file (ID '6', path '/home/vsts/work/1/s/Authorization/Class.cs', NO INDEXED PATH), line '13', visitCount '0' because file is not indexed or does not have the supported language.
...
14:22:15.680 DEBUG: Analyzing coverage after aggregate found '0' coverage files.

14:22:15.680 DEBUG: The total number of file count statistics is '0'.

Hi,

Can you provide the full log?

Since your source code (versus tests) isn’t being detected, I’m not terribly interested in the detection / parsing of your coverage report. As I stated earlier, without source files, it’s quite natural that you don’t have any coverage since you can’t have coverage of tests.

 
Ann

How can I send u the logs privately somehow, cuz I just cannot show it here.
What could cause that the coverage is only taking the test projects, do you have any idea?

Hi,

Feel free to redact the sensitive data.

 
Ann

I actually solved it, the final version of pipeline:

trigger:
- dev
- main

pool:
  vmImage: 'windows-latest'

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

jobs:
- job: check_code_quality
  steps:
  - task: DotNetCoreCLI@2
    displayName: 'Restore solution'
    inputs:
      command: 'restore'
      projects: '**/*.sln'
      feedsToUse: 'select'

  - task: SonarCloudPrepare@1
    displayName: 'Prepare analysis configuration'
    inputs:
      SonarCloud: 'SonarCloud'
      organization: '***'
      scannerMode: 'MSBuild'
      projectKey: '***'
      projectName: '***'
      extraProperties: |
        sonar.exclusions=**/obj/**,**/*.dll
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
        sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
        sonar.verbose=true

  - task: DotNetCoreCLI@2
    displayName: 'Build solution'
    inputs:
      command: 'build'
      projects: '**/*.sln'

  - task: DotNetCoreCLI@2
    displayName: 'Execute Unit tests'
    inputs:
      command: 'test'
      projects: '**/*[Tt]est*/*.csproj'
      arguments: '--configuration Release --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover'
      publishTestResults: true
  
  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage report'
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
      
  - task: SonarCloudAnalyze@1
    displayName: 'Run SonarCloud analysis'

  - task: SonarCloudPublish@1
    displayName: 'Publish results on build summary'
    inputs:
      pollingTimeoutSec: '300'
1 Like

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