Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Coverage and Technical Debt

Code Coverage in SonarQube: Line and Condition Coverage

Understand how SonarQube measures coverage, the difference between line and condition coverage, coverage on new code, and exclusions, with a calculator you can run.

SonarQube does not run your tests and does not measure coverage by itself. Your build runs the tests with a coverage tool, writes a report, and the scanner imports that report. SonarQube then shows the numbers next to the source code, tracks them over time, and can make the quality gate fail when coverage on new code is too low.

This lesson covers what the numbers mean. The next lesson, Importing coverage reports, shows the properties for each language. SonarQube is not runnable here, so the TryIts are small calculators that reproduce its formulas.

What coverage tells you (and does not)

Coverage says which lines and branches were executed during tests. It does not say whether the tests checked anything. A test that calls a function and asserts nothing gives full coverage and no protection. Use coverage as a floor: low coverage means untested code; high coverage means little about test quality. Combine it with reviews and, where you can, mutation testing.

The metrics

Metric key Meaning
lines_to_cover Executable lines (not comments or blank lines).
uncovered_lines Executable lines no test ran.
line_coverage Covered lines divided by lines to cover.
conditions_to_cover Number of branch outcomes (each if has two).
uncovered_conditions Branch outcomes never taken.
branch_coverage Covered conditions divided by conditions to cover.
coverage Overall percentage combining both.
new_coverage The same, only for new code.

The overall coverage formula is:

TEXT
coverage = (CT + CF + LC) / (2 * B + EL)

where CT is conditions that were evaluated to true at least once, CF conditions evaluated to false at least once, LC covered lines, B the total number of conditions and EL the executable lines. The 2 * B appears because each condition has two outcomes.

A worked example: 100 executable lines, 90 covered. Ten if statements, so B = 10 and 20 outcomes, of which 14 were taken. Then coverage = (14 + 90) / (20 + 100) = 104 / 120 = 86.7%, while line coverage alone is 90%. Branches pull the number down, which is the point: an if with only its "true" side tested is half-tested.

JS Stand-in: SonarQube coverage formula

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that Module B has no conditions, so branch coverage is not applicable and overall equals line coverage.

Coverage on new code

Because the Clean as You Code approach judges new code, the quality gate usually checks new_coverage, not total coverage. New code is defined by the new code period (for instance, since a reference branch or the previous version). The rule of the built-in Sonar way gate is coverage on new code of at least 80%.

Only lines changed or added in the period count. A pull request that touches 50 lines, 40 of which are covered, has 80% new coverage no matter how poorly the other 5,000 lines of the project are tested. That keeps the target realistic for old projects.

flowchart LR
  A["Run tests with coverage tool"] --> B["Write report file"]
  B --> C["Scanner imports report"]
  C --> D["Server joins report with new-code lines"]
  D --> E["new_coverage compared with gate"]

Figure: coverage data flows from the build to the server; the server decides which lines are new.

Stand-in: coverage on new lines only

This block takes per-line data and a list of "new" lines and computes both numbers, so you can see how the same report gives different answers for the whole file and for new code.

JS Stand-in: overall vs new-code coverage

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The file looks decent overall (5 of 8 executable lines are covered), but three of the four new lines are uncovered, so the gate fails. That is the typical conversation a coverage gate starts: "add a test for the code you just wrote".

Excluding code from coverage

Some code is not worth measuring: generated sources, configuration classes, data transfer objects. Use these analysis properties (in sonar-project.properties, the build tool or the project's Administration > General Settings > Analysis Scope page):

  • sonar.coverage.exclusions: files that stay in the project but do not count toward coverage. Example: **/dto/**,**/*Config.java.
  • sonar.exclusions: files that are not analysed at all (no issues either).
  • sonar.test.inclusions and sonar.tests: tell SonarQube which files are tests.

Patterns use Ant-style globs (** for any directories, * within a name). Excluding to hit a number is a smell; exclude only what you would not write unit tests for.

Common problems

  • Coverage shows 0% or "no coverage". The report path is wrong, or the tests ran after the scan. Look in the scanner log for lines about "Sensor ... coverage" or "No report found".
  • Coverage looks lower than in the IDE. SonarQube counts conditions; some IDE views show lines only. Branch coverage also differs between tools.
  • Paths do not match. The report lists paths from a different working directory (very common in Docker). Fix the path in the report or the sonar.sources setting.
  • A module is missing. Multi-module projects need each module's report listed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Does SonarQube execute your tests to measure coverage?

    SonarQube only imports coverage reports produced by your own test run.
  2. 2. With 50 executable lines (40 covered) and 5 conditions (6 of 10 outcomes taken), what is the overall coverage?

    (6 + 40) / (10 + 50) = 46 / 60 = 76.7%.
  3. 3. Which property removes files from coverage but still analyses them for issues?

    sonar.coverage.exclusions affects only the coverage measure.
  4. 4. What does the default gate check for coverage?

    Sonar way applies an 80% threshold to coverage on new code.

Summary

  • Coverage comes from your build's report; SonarQube imports and displays it.
  • Overall coverage combines line and condition outcomes; new_coverage looks only at changed lines.
  • The default gate asks for 80% on new code.
  • Use sonar.coverage.exclusions for code that is not meaningfully testable.

Next: Importing coverage reports