Code Coverage is not shown in the SonarQube


I have SonarQube installed on a Windows agent in Jenkins.

I want to see code coverage percentage and unit tests in SonarQube. I used the official documentation for all the commands.

Here is my example:

withSonarQubeEnv(credentialsId: 'mycred', installationName: 'SonarQube') {
    def scannerHome = tool 'SonarScanner'
    bat "dotnet ${scannerHome}\\SonarScanner.MSBuild.dll begin /k:\"${proj_key}\" /\"${env.SONAR_LINK}\" /d:sonar.cs.opencover.reportsPaths=\"tests\\TestResults\\*.xml\" /d:sonar.cs.vstest.reportsPaths=\"**/*.trx\" -v:diag"
    bat "dotnet build --no-incremental"
    bat "dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput='./TestResults/'"
    bat "dotnet test --collect:\"XPlat Code Coverage\""
    bat "dotnet ${scannerHome}\\SonarScanner.MSBuild.dll end"

In Jenkins, a folder tests\\TestResults is created, where I see test_results.trx, and it shows me the tests in SonarQube - there are no problems with that.

For code coverage, two files are created:

  • tests\TestResults\c377z55b-poq3-421e-8bad-0dc790a2841cAdministrator_JENKINS-AGENT_2024-04-10.12_57_33.coverage and
  • tests\TestResults\c117f25b-fbc3-421e-8bad-0dc593f2839b\coverage.cobertura.xml

I don’t understand which of these files should be passed to SonarQube and how to display the percentage there?

SonarQube works only with one branch for me. Please advise.

Hey there.

As noted in the template post, what version of SonarQube are you using?

And, what do the logs say about importing code coverage?

SonaQube version is * Community Edition* Version 10.3 (build 82913)
SonarScanner for .Net 6.0.0

17:30:28 INFO: Parsing the OpenCover report C:\jenkins-agent\workspace\pipelines.\tests\TestResults\SonarQube.xml 17:30:28 WARN: Could not import coverage report ‘.\tests\TestResults\SonarQube.xml’ because ‘Missing root element in C:\jenkins-agent\workspace\pipelines.\tests\TestResults\SonarQube.xml at line 2’.


How is that SonarQube.xml file getting generated? I wouldn’t expect it to be the output of these commands (the name isn’t specified anywhere)

First things first, if this file isn’t expected, I would make sure it stops getting generated. Don’t forget to check if it somehow got checked into your repo as well.