Coverage data in sonarqube is different from that in cobertura

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  • how is SonarQube deployed: zip, Docker, Helm
  • what are you trying to achieve
    using cobertura to report C# projects to sonarqube, the data in sonarqube is different with that in cobertura
  • what have you tried so far to achieve this

Hey there.

You’ll need to give some more details than that. What is the nature of the differences? Screenshots and logs are super helpful here.

I would guess they are referring to the fact that the SonarQube algorithm for computing “Coverage” is not the same as line coverage in Cobertura or JaCoCo. From a high-level, SonarQube uses a “weighted average” of line coverage and condition coverage. Colin can provide more details.

Hey Colin,
I can paste some of our azure pipeline configuration, we are using this pipeline to collect coverage , it’s not compliant to paste all of them here, so I rename or change some keywords.

          - task: SonarQubePrepare@5
            displayName: Prepare SonarQube Analysis
              SonarQube: My Sonar
              projectKey: XXX
              projectName: XXX
              projectVersion: $(Build.BuildNumber)
              extraProperties: |

          - task: Bash@3
                        targetType: 'inline'
                        script: |
                          set -x
                          dotnet test $(Build.SourcesDirectory)/src/Tests/Scan.Tests/XXX.Tests.csproj --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover --logger:"trx" --settings:"$(Build.SourcesDirectory)/src/Tests/update.runsettings"
                          if [ $? -ne "0" ]; then
                              echo Failed.
                              exit 1

           - task: PublishCodeCoverageResults@1
            displayName: 'Publish code coverage results'
              codeCoverageTool: Cobertura
              summaryFileLocation: '$(Build.SourcesDirectory)/coverlet/Cobertura.xml'

          - task: SonarQubeAnalyze@5
            displayName: SonarQube Analysis

          - task: SonarQubePublish@5
            displayName: Publish Quality Gate Result

This is one of solutions that I can see data for coverage in sonarqube, however the coverable lines and code coverage data is not the same. For this project, in sonar coverable lines are 13k, and coverage is 79%, in cobertura result, coverable lines are 16k and coverage is 78%. In this project, the difference is not so big, but I config this in another C# project, it’s greater differences(60% vs 40%).

I got two questions, one is that why there is differences between opencover format result and cobertura result. And another is that given we are using cobertura to calculate coverage, is it possible to directly use cobertura result to sonar, then there will be no confusion.(I also tried this way, using sonar.flex.cobertura.reportPaths to import cobertura files, it’s not working so well, showing multiple files found…)

@Colin Hi, can you help clarify the differences between SonarQube’s weighted average coverage mentioned by David and Cobertura or JaCoCo? Thanks

Hey @QingyuanFan

What David is referring to is the combination of line/branch coverage documented here.

1 Like