# Different coverage percentage on SonarQube dashboard and coverage report generated by coverlet

Must-share information (formatted with Markdown):

• Dotnet Sonar Scanner version - 6.2.0
• how is SonarQube deployed: Docker
• what are you trying to achieve: Want to know how exactly is the coverage % calculated by Sonar Scan and displayed on the SonarQube Dashboard

Project Structure

We have 2 workflows

1. Unit Test Coverage workflow - which uses @action/coverlet to generate a coverage report for a project
2. Sonar Scan workflow - It generates the coverage report in the Build & Analyze step of Sonarscan and uploads the coverage % on the SonarQube Dashboard

For the Unit Test Coverage workflow,

Coverage Report Summary:

Column 1 Column 2 Column 3 Column 4
Module Line Coverage Branch Coverage Method Coverage
CameraSystemService 97.05% 88.88% 92.85%

Final Total Line Coverage: 97%

While for the SonarScan coverage workflow

The Coverage % = 92%

When I printed the coverage report in Unit Test Coverage workflow and Sonar Scan workflow, the content appeared almost same

I read this discussion,

where David mentions that SonarQube uses a “weighted average” of line and branch coverage

So as the

Line Coverage: 97.05%

Branch Coverage: 88.88%

Assuming equal weights for line and branch = 0.5

Weighted Average=(Line Coverage × Weight for Line)+(Branch Coverage × Weight for Branch)

= 97.05 * 0.5 + 88.88*0.5

= 92.94

I want to know is this how the coverage is calculated in SonarScan and is that why I am getting the coverage % as 92% in SonarQube dashboard??

Hi,

You’ll find the docs and this dusty-but-still-relevant blog post helpful.

Ann

Hello @ganncamp

For my case for line coverage
(132 visited out of 136 total) = 97.05%

And for Branch coverage
(16 branch out of 18 branch ) covered = 88.88%
numSequencePoints=“136” visitedSequencePoints=“132” numBranchPoints=“18” visitedBranchPoints=“16”

In the coverage report we only have visitedBranchPoints and not CT,CF(conditions that have been evaluated to ‘true’/‘false’ at least once). Then how can I use this formula

I also read the document on “Why did my coverage just drop?” and understood that how sonarscan not only focuses on line coverage but a new metrics called as Executable Lines

Hi,

I suppose `visitedBranchPoints` is equal to `CT+CF`.

HTH,
Ann

Okay thanks for your reply. If I consider

visitedBranchPoints = CT + CF = 16
And 2B = numBranchPoints=“18”

Then I get the condition coverage = 88.88%

Where LC = 132; EL = 136; CT + CF = 16; 2B = 18
While if I substitute the values in the Final Equation = (CT + CF + LC)/(2
B + EL)
= 96.10 %

But in the SonarQube dashboard coverage % = 92%

I also check that only the required files are getting checked and excluded file is covered by the Sonarscan

In the Measures > Coverage

But actually the numbers of line of code is more than 15. It is around 136

I am creating a new pull request. I worked on adding only 1 new line of code

and which is also tested

I don’t know why does it say that it has only 15 lines to cover

Hi,

There are 136 coverable lines of code in your PR? Not comments, not whitespace, not imports &etc, but coverable lines of code?

Ann

Hii.

I think I got it now. The unittestcoverage workflow is running on all lines of code and then providing the line and branch coverage. While the sonarscan runs only on the modified code and then it calculates the coverage using the above mentioned formula

I check my code changes and the lines changes was aprox. 15. Then I believe that the coverage % what we are getting from sonarqube dashboard is just for the new committed code

1 Like