# How coverage metrics is calculated

Hello,

I’m trying to understand how exactly coverage metrics is calculated.
As described here Metric Definitions | SonarQube Docs
there is a formula:

`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 = lines to cover - uncovered_lines
• B = total number of conditions
• EL = total number of executable lines ( `lines_to_cover` )

I have a java class covered by tests, here are the numbers:
Coverage: 69.2%
Lines to Cover: 22
Uncovered Lines: 8
Line Coverage: 63.6%
Conditions to Cover: 4
Uncovered Conditions: 0
Condition Coverage: 100%

As far as I understand this correctly, it must be equal to 73.3% (4+4+14/2*4+22)
Can someone explain me how coverage ended up with number of 69.2% ?

Okay it looks like I take wrong number of branches. There are 2 branches in this class so the correct calculations are: (2+2+14/2*2+22)=69.2%

But I still don’t understand how SonarQube gets the number of branches from the jacoco report which states next numbers:

``````INSTRUCTION_MISSED: 37
INSTRUCTION_COVERED: 84
BRANCH_MISSED: 0
BRANCH_COVERED: 4
LINE_MISSED: 8
LINE_COVERED: 14
COMPLEXITY_MISSED: 2
COMPLEXITY_COVERED: 6
METHOD_MISSED: 2
METHOD_COVERED: 4
``````

How SonarQube determines the number of branches?

Okay it looks like number of branches (B) = (BRANCH_MISSED+BRANCH_COVERED) / 2

1 Like