# Code Coverage calculation in Sonar

**URL:** <https://community.sonarsource.com/t/code-coverage-calculation-in-sonar/67482>\
**Category:** SonarQube Server / Community Build\
**Tags:** coverage\
**Created:** [July 5, 2022, 12:37pm UTC](https://community.sonarsource.com/t/code-coverage-calculation-in-sonar/67482 "2022-07-05T12:37:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Pires](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/marc_pires/32/34795_2.png) [@Marc\_Pires](https://community.sonarsource.com/u/Marc_Pires)\
**Post date:** [July 5, 2022, 12:37pm UTC](https://community.sonarsource.com/t/code-coverage-calculation-in-sonar/67482/1 "2022-07-05T12:37:59Z")

</div>

Hi everyone, I have a NodeJS 14 project on Sonar Enterprise 9.4 (scanner version 4.7.0.2747),  
Here is the config for scanner and also for the test using mocha and istanbul.

````auto
sonar.sources="lib"
sonar.language="js"
sonar.javascript.lcov.reportPaths="coverage/lcov.info"
sonar.exclusions="lib/gen-nodejs/ **,lib/payload/protobuf/Gateway_pb.js,examples/** ,node_modules/ **,test/** ,"

package.json scripts

```json
"scripts": {
 "test": "istanbul cover --report html node_modules/.bin/_mocha -- -t 600000 recursive $(find ./test -path '*.js')",
"cover": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -u exports -R spec" ,
"cobertura": "./node_modules/.bin/istanbul report --dir ./coverage/report cobertura"
}

````

According the docs Sonar uses the following model to calculate coverage:

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)**

Following the formula above, how would fit for the file below

 ![example_coverage](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/5/f/5fc3f874eff40f3aaf2224e30fbfea5442d450ef.jpeg)

**file measures**

 ![file_measures](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/b/d/bd5cb5c0370b2a88638c0c784faefaec3608ba3a.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [July 5, 2022, 1:04pm UTC](https://community.sonarsource.com/t/code-coverage-calculation-in-sonar/67482/2 "2022-07-05T13:04:09Z")

</div>

CT+CF = 1 (2 Conditions to Cover - 1 Uncovered Conditions)  
LC = 7 (8 Lines to Cover - 1 Uncovered Line)  
2\*B = 2 (Conditions to Cover already represents the product)  
EL = 8 (8 Lines to Cover)

(1 + 7)/(2+8)

8/10 = 80%

---

<div class="post-metadata">

**Author:** ![Marc\_Pires](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/marc_pires/32/34795_2.png) [@Marc\_Pires](https://community.sonarsource.com/u/Marc_Pires)\
**Post date:** [July 5, 2022, 1:05pm UTC](https://community.sonarsource.com/t/code-coverage-calculation-in-sonar/67482/3 "2022-07-05T13:05:28Z")

</div>

Thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![Mr\_Jhon](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mr_jhon/32/77654_2.png) [@Mr\_Jhon](https://community.sonarsource.com/u/Mr_Jhon)\
**Post date:** [September 28, 2026, 7:06pm UTC](https://community.sonarsource.com/t/code-coverage-calculation-in-sonar/67482/4 "2026-09-28T19:06:22Z")

</div>

The formula makes sense, but I think the confusing part here is how SonarQube is interpreting the covered conditions and executable lines from the LCOV report. I’d first compare the `lines`, `functions`, and `branches` sections in `coverage/lcov.info` with the measures shown in SonarQube. That should help identify whether the difference comes from the report itself or from how SonarQube maps the coverage data to the source file. Checking a small example file manually can also make the calculation much easier to verify.
