SonarQube Code Coverage Reports

Code coverage tells you how much of your program the tests actually run. A high number means tests touch most lines. A low number means large parts of the program run in production without any safety net. SonarQube displays coverage but needs a separate tool to measure it.

The Spotlight Picture

Imagine a dark warehouse and a guard who walks the aisles with a flashlight. Lit aisles stand for tested code, and dark aisles stand for untested code. Coverage measures the share of aisles the flashlight reached.

 Warehouse (your code)
 ┌─────┬─────┬─────┬─────┬─────┐
 │ ███ │ ███ │ ███ │ ░░░ │ ░░░ │   ███ = tested by a test run
 │ ███ │ ███ │ ░░░ │ ░░░ │ ███ │   ░░░ = never executed
 └─────┴─────┴─────┴─────┴─────┘
 Coverage = 6 lit blocks ÷ 10 blocks = 60%

Who Does What

  1. A test runner executes your tests.
  2. A coverage tool records which lines ran and writes a report file.
  3. The scanner uploads that file together with the source analysis.
  4. SonarQube merges the numbers and shows them on the dashboard.

SonarQube never runs tests. Missing reports always produce a coverage value of zero or no value at all.

Coverage Types

Line Coverage

Line coverage counts executed lines out of all executable lines.

Condition Coverage

Condition coverage checks whether both outcomes of each decision ran, such as the true and false sides of an if statement. A line can run while one branch stays untested, so this measure catches hidden gaps.

Combined Coverage

The dashboard shows one blended percentage that uses both lines and conditions.

Report Setup per Language

LanguageCoverage ToolSonarQube Property
JavaJaCoCosonar.coverage.jacoco.xmlReportPaths
JavaScript / TypeScriptJest or nyc (lcov)sonar.javascript.lcov.reportPaths
Pythoncoverage.py (XML)sonar.python.coverage.reportPaths
C#Coverlet or dotCoversonar.cs.opencover.reportsPaths

Python Example

coverage run -m pytest
coverage xml
sonar-scanner -Dsonar.python.coverage.reportPaths=coverage.xml

JavaScript Example

npx jest --coverage
sonar-scanner -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info

Excluding Files From Coverage

Some files never need tests, such as configuration classes and generated code. Remove them from the math with a dedicated property.

sonar.coverage.exclusions=**/config/**,**/generated/**

The files stay in analysis for bugs and smells. Only the coverage calculation ignores them.

Coverage on New Code

The default quality gate demands at least 80% coverage on New Code. This rule rewards teams that test fresh changes and spares them from fixing years of old gaps all at once. Overall coverage then rises naturally as files get updated.

Reading the Coverage Tab

  1. Open the project and select Measures.
  2. Choose Coverage from the list.
  3. Sort files by uncovered lines.
  4. Open a file to see green and red markers beside each line.

Good Habits

  • Test behavior that matters instead of chasing a perfect number.
  • Write assertions. A test with no assertion raises coverage but proves nothing.
  • Review red lines in critical code such as payments and login first.
  • Keep the report path stable between runs.

Troubleshooting

  • Coverage shows 0% means SonarQube found no report. Check the path and run tests before the scan.
  • Wrong file paths in the report prevent matching. Generate the report on the same machine and folder as the scan.
  • Numbers differ from the local tool when exclusions differ. Compare the exclusion lists.

A Short Story

A developer adds a discount function with three branches: no discount, member discount, and coupon discount. The test suite checks only the member case. The coverage tool reports the function as partly covered, and SonarQube shows red marks beside the other two branches. The developer writes two more tests, and the marks turn green. A hidden coupon bug appears during that process, and the team fixes it before release. Coverage reports guide attention to the places where bugs wait quietly.

Quick Recap

Tests run, a coverage tool writes a report, the scanner uploads it, and SonarQube displays the result. Aim for steady coverage on new code and meaningful tests everywhere.