Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Coverage and Technical Debt

Importing Coverage Reports: JaCoCo, LCOV, Cobertura and More

Configure SonarQube to import coverage from JaCoCo, LCOV, Cobertura, OpenCover and the generic format, with property names per language and a parser stand-in you can run.

Each language ecosystem has its own coverage tool and report format, and SonarQube reads the common ones through language-specific properties. For anything it does not read natively there is a generic coverage format, a small XML file you can produce from any tool. This lesson lists the properties, explains the order of steps, and parses two report formats in a stand-in.

SonarQube itself is not available on this site. The properties and XML below are real configuration you would use on your own server, but the TryIt blocks are only stand-ins.

The golden rule: tests first, scan second

The scanner reads files that already exist. The pipeline order is always:

  1. Build and run tests with coverage enabled.
  2. Confirm the report file is on disk.
  3. Run the scanner.

If you scan first, the report is missing and coverage shows as empty.

Properties per language

All of these go into sonar-project.properties, the Maven/Gradle configuration, or -D arguments.

Ecosystem Tool and format Property
Java, Kotlin JaCoCo XML sonar.coverage.jacoco.xmlReportPaths
JavaScript, TypeScript LCOV (lcov.info) sonar.javascript.lcov.reportPaths (JS) and the same property is used for TS projects
Python Cobertura XML from coverage xml sonar.python.coverage.reportPaths
C# / .NET OpenCover, dotCover HTML, VS Coverage XML sonar.cs.opencover.reportsPaths, sonar.cs.dotcover.reportsPaths, sonar.cs.vscoveragexml.reportsPaths
PHP Clover or PHPUnit sonar.php.coverage.reportPaths
Go go test -coverprofile sonar.go.coverage.reportPaths
Anything else Generic test coverage XML sonar.coverageReportPaths

Paths can be comma-separated lists and may use wildcards. Relative paths are resolved from the project base directory.

Example: Java with Maven and JaCoCo

In pom.xml the JaCoCo plugin has prepare-agent and report goals. Then:

Terminal
mvn verify
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
  -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml

The Maven scanner uses the default JaCoCo location automatically in many setups, but naming the path explicitly makes the pipeline easier to debug.

Example: JavaScript with Jest

Terminal
npx jest --coverage --coverageReporters=lcov

This writes coverage/lcov.info. The properties file contains:

PROPERTIES
sonar.projectKey=shop-frontend
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.test.ts,**/*.test.tsx
sonar.javascript.lcov.reportPaths=coverage/lcov.info

Example: Python with coverage.py

Terminal
coverage run -m pytest
coverage xml

That creates coverage.xml (Cobertura format), which you reference with sonar.python.coverage.reportPaths=coverage.xml.

The generic coverage format

When the tool is unusual, write this file yourself or convert to it:

XML
<coverage version="1">
  <file path="src/main/java/com/shop/Cart.java">
    <lineToCover lineNumber="6" covered="true"/>
    <lineToCover lineNumber="7" covered="false"/>
    <lineToCover lineNumber="9" covered="true" branchesToCover="2" coveredBranches="1"/>
  </file>
</coverage>

path is relative to the project base directory or absolute. branchesToCover and coveredBranches are optional. Point to it with sonar.coverageReportPaths=coverage-generic.xml.

Stand-in: parse an LCOV report

LCOV is a plain text format: SF: starts a file, DA:line,hits records one line, end_of_record closes the file. The block below parses a small sample and prints coverage per file, imitating what the scanner does before sending data to the server.

JS Stand-in: parse LCOV and compute line coverage

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

No output.

Stand-in: convert LCOV to the generic format

Writing a converter is a realistic task when a tool has no native support. This version builds the generic XML text from the parsed lines.

JS Stand-in: LCOV to generic coverage XML

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

No output.

Checking that the import worked

  1. In the scanner log, look for lines mentioning the coverage sensor and the number of files with coverage.
  2. In the project, open Measures > Coverage and drill down to files; a file with no data shows "no coverage information" instead of 0%.
  3. If a whole module is blank, check paths in the report against sonar.sources; Docker builds often use /app/... paths in reports while the scanner sees a different directory.
  4. Make sure test files are not also counted as sources. Set sonar.tests and sonar.test.inclusions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which property imports JaCoCo XML for a Java project?

    JaCoCo reports are read through sonar.coverage.jacoco.xmlReportPaths.
  2. 2. A scan runs before the tests finish. What happens to coverage?

    The scanner only reads files that already exist, so tests must run first.
  3. 3. Which format would you use for a coverage tool SonarQube does not support?

    The generic XML format exists so any tool's data can be converted and imported.
  4. 4. In an LCOV file, what does a DA:3,0 line mean?

    DA records line number, then the number of hits.

Summary

  • Run tests with coverage first, then the scanner; SonarQube imports, it does not measure.
  • Use the language-specific report property, or the generic XML for other tools.
  • Debug missing coverage by checking the scanner log, report paths and source paths.
  • Exclude non-testable code with sonar.coverage.exclusions.

Next: Technical debt and the A-to-E ratings