How is code coverage calculated for subdirectories for average

Hi Team,

I need clarification on how sonar cube code coverage % value is displayed on the dashboard.
For example.

If Project A dashboard has below data.

Folder SRC Code coverage 43%

now when i navigate inside SRC folder in sonarqube portal dashboard i can see below breakup of code coverage against each folder.

SRC (root level) Code coverage 43%
-inc 68%
-xxx 45%
-yyy 35%
-kkk 58%

Now with these data inside SRC folder how to know if the final code coverage % is calculated correctly ? is there any formula to calculate Codecoverage % at root level.

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube): * Enterprise Edition
  • Version 9.9 (build 65466)
  • how is SonarQube deployed: Docker

Hey there.

The overall code coverage will consider the overall coverage measures you can see in your project’s Measures > Coverage > Overall Code, plugged into this formula:

Coverage (coverage): A mix of Line coverage and Condition coverage. It’s goal is to provide an even more accurate answer the question ‘How much of the source code has been covered by the unit tests?’.

Coverage = (CT + CF + LC)/(2*B + EL)
where:

  • CT = conditions that have been evaluated to ‘true’ at least once
  • CF = conditions that have been evaluated to ‘false’ at least once
  • LC = covered lines = linestocover - uncovered_lines
  • B = total number of conditions
  • EL = total number of executable lines (lines_to_cover)

Larger folders will have more weight, naturally, as they have more lines of code / conditions.

Thanks for the response. let me try to explain my question with actual issue. For example if i have following 12 subfolders under root dir (SRC) which currently shows coverage as 87.5%
SRC 87.5% { Total }

|--aaa 				    100%
|--bbb                  95.20%
|--ccc				    88.20%
|					    98.80%
|					    81.90%
|                       98.20%
|                       85.90%
|					    87.90%
|					    83.90%
|					    100%
|					    84.60%
|--ZZZ			        86.40%

If i take the avg of above 12 rows of dirs i am getting the avg of 90.92% but in the portal its showing 87.5% so i am not able to understand how the calculation is being done here to get 87.5%

I imagine that not every folder has exactly the same amount of code. Averaging the directories will require that you take this into account.

Put another way – if you have two folders, one with 100 lines of code and 100% coverage, and another with 1 lines of code and 0% coverage… you’re average coverage isn’t 50%. It’s 99%.

1 Like

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