Rendering Code Coverage Report on Sonarqube and Azure DevOps

Lakshaykaushik
2 min readDec 4, 2020

Rendering code coverage reports on Sonarqube and Azure Devops is a common expectation while we are creating CI pipelines.

Although, its very easy to publish code coverage reports on azure devops using “PublishCodeCoverageResults@1” task, but there is a limitation here of azure devops, it cannot publish opencover.xml code coverage reports.

Also, Sonarqube doesn't support Cobertura code coverage reports.

So, how do you publish the code coverage reports on both azdo and Sonarqube.

One way to do this is:

  1. Run the Unit test cases.
  2. Convert the coverage report generated in opencover format to Cobertura format using the open source tool reportgenerator.
  3. You will now have 2 format of reports, one supported by azdo and one supported by Sonarqube.

Step 1: Run the Unit test cases

# Run Unit tests- task: DotNetCoreCLI@2displayName: 'Run Unit tests'inputs:command: testprojects: '**/src/Tests/**/*.csproj'arguments: '--configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=./TestResults/'

This creates report in opencover format which is supported by sonarqube but not by azdo.

Step 2: Convert thus report into Cobertura format for azure devops

- script: |dotnet tool install dotnet-reportgenerator-globaltool --tool-path ../reportgenerator "-reports:$(Build.SourcesDirectory)/**/TestResults/coverage.opencover.xml" "-targetdir:coverage/Cobertura" "-reporttypes:Cobertura;HTMLInline;HTMLChart"displayName: Generate Coverage Report

Step 3: Publish Code coverage report on azdo

# Publish code coverage to azdo- task: PublishCodeCoverageResults@1displayName: 'Publish Code Coverage Results'inputs:codeCoverageTool: 'cobertura'summaryFileLocation: '$(Build.SourcesDirectory)/coverage/Cobertura/Cobertura.xml'failIfCoverageEmpty: true

Step 4: Publish Code coverage report on Sonarqube

- task: SonarSource.sonarqube.15B84CA1-B62F-4A2A-A403-89B77A063157.SonarQubePrepare@4displayName: 'Prepare analysis on SonarQube'inputs:SonarQube: 'SonarName'projectKey: 'SonarKey'projectName: 'SonarProject'scannerMode: 'MSBuild'extraProperties: |sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/TestResults/coverage.opencover.xml- task: SonarSource.sonarqube.6D01813A-9589-4B15-8491-8164AEB38055.SonarQubeAnalyze@4displayName: 'Run Code Analysis'- task: SonarSource.sonarqube.291ed61f-1ee4-45d3-b1b0-bf822d9095ef.SonarQubePublish@4displayName: 'Publish Quality Gate Result'

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Lakshaykaushik
Lakshaykaushik

Responses (2)

Write a response