Test Coverage & Execution

Test coverage reports and test execution reports are important code quality metrics that you can import into SonarQube. Test coverage reports tell you the percentage of your code that is covered by your test cases. Test execution reports tell you which tests have been run and their results.

SonarQube doesn't run your tests or generate reports. To include coverage results in your analysis, you need to set up a third-party coverage tool to generate reports and configure SonarQube to import those reports.

Below you'll find guidelines and resources as well as well as language- and tool-specific analysis parameters for importing coverage and execution reports.

General Guidlines

Before importing test coverage and execution reports, you need to have the appropriate SonarScanner configured as part of your build pipeline. Then, complete the following steps:

  1. Set up your coverage tool to run as part of your build pipeline. Your coverage tool should be set up to run before the SonarScanner analysis.
  2. Configure the coverage tool so that location and format of the output report files matches what the SonarScanner expects.
  3. Configure the analysis parameters of the SonarScanner so that it can import the report files.

Now, on each build of your project, your coverage tool performs its analysis and outputs its results to one or more files (usually one for test coverage and one for test execution). Then, the SonarScanner imports those files and sends the results to SonarQube as part of its analysis process.

Supported report formats

SonarQube supports a variety of formats from third-party tools. We also provide a generic test data format to easily gather and import test coverage and execution information into SonarQube.

Language-specific guides

On the SonarSource Community forum, you can find guides with instructions on generating reports for the following languages:

Wildcards

Some analysis parameters support the following path wildcards. Properties that support wildcards are noted in the Description columns of the analysis parameter tables below:

SymbolMeaning
?a single character
*any number of characters
**any number of directories

Test coverage analysis parameters

Unless otherwise specified, these properties require values that are relative to project root.

All Languages

Analysis parameterDescription
sonar.coverageReportPathsPath to coverage report in the Generic Test Data format.

Apex

Analysis parameterDescription
sonar.apex.coverage.reportPathPath to the test-result-codecoverage.json report file generated by the apex:test:run command of the Salesforce CLI. Note, you must have a Salesforce DX project set up and linked to your Org

C/C++/Objective-C

Analysis parameterDescription
sonar.cfamily.gcov.reportsPathPath to the directory containing native *.gcov reports (not the XML reports generated by gcovr)
sonar.cfamily.llvm-cov.reportPathPath to a llvm-cov report
sonar.cfamily.vscoveragexml.reportsPathPath may be absolute or relative to the solution directory. Path wildcards (see above) are supported. Note that the .coveragexml report format offered by Visual Studio is not supported.
sonar.cfamily.bullseye.reportPathPath to the report from Bullseye, version >= 8.9.63 (use covxml tool)

C#

Analysis parameterDescription
sonar.cs.vscoveragexml.reportsPathsPath to Visual Studio Code Coverage report. Multiple paths may be comma-delimited, or included via wildcards. See Notes on importing .NET reports below.
sonar.cs.dotcover.reportsPathsPath to dotCover coverage report. See Notes on importing .NET reports below.
sonar.cs.opencover.reportsPathsPath to OpenCover coverage report. See Notes on importing .NET reports below.
sonar.cs.ncover3.reportsPathsDeprecated. Path to NCover3 coverage report. See Notes on importing .NET reports below.

Flex

Analysis parameterDescription
sonar.flex.cobertura.reportPathsPath to the Cobertura XML reports. Multiple paths may be comma-delimited. May be absolute or relative to the project base directory.

Go

Analysis parameterDescription
sonar.go.coverage.reportPathsComma-delimited list of paths to coverage report files. Path wildcards are supported (see above) since SonarGo 1.1.

Java, Kotlin, Scala, and JVM

Analysis parameterDescription
sonar.coverage.jacoco.xmlReportPathsPath to JaCoCo XML coverage reports. Path wildcards are supported (see above).

Javascript and Typescript

Analysis parameterDescription
sonar.javascript.lcov.reportPathsComma-delimited list of paths to LCOV coverage report files. Paths may be absolute or relative to project root.

PHP

Analysis parameterDescription
sonar.php.coverage.reportPathsComma-delimited list of paths to Clover XML-format coverage report files. Paths may be absolute or relative to project root.

Python

Analysis parameterDescription
sonar.python.coverage.reportPathsComma-delimited list of paths to coverage reports in the Cobertura XML format. Path wildcards are supported (see above). Leave unset to use the default (coverage-reports/*coverage-*.xml).

Ruby

Analysis parameterDescription
sonar.ruby.coverage.reportPathsComma-delimited list of paths to SimpleCov report files generated with the JSON formatter (availaible from SimpleCov 0.20). For SimpleCov versions < 0.18, you can provide .resultset.json report files (not recommended). Paths may be absolute or relative to project-root.

Scala

Analysis parameterDescription
sonar.scala.coverage.reportPathsComma-separated list of paths to scoverage.xml report files generaged by Scoverage.

Swift, Xcode 9.3+

Analysis parameterDescription
 You can use the xccov-to-sonarqube-generic.sh script from the sonar-scanning-examples/swift-coverage project convert output from Xcode 9.3's xccov tool to the Generic Test Data format.

Swift, Xcode 7–9.2

Analysis parameterDescription
sonar.swift.coverage.reportPathPath to the report generated by llvm-cov show. Path may be absolute or relative to project root.

VB.NET

Analysis parameterDescription
sonar.vbnet.vscoveragexml.reportsPathsPath to Visual Studio Code Coverage report. Multiple paths may be comma-delimited, or included via wildcards. See Notes on importing .NET reports below.
sonar.vbnet.dotcover.reportsPathsPath to dotCover coverage report. See Notes on importing .NET reports below.
sonar.vbnet.opencover.reportsPathsPath to OpenCover coverage report. See Notes on importing .NET reports below.
sonar.vbnet.ncover3.reportsPathsDeprecated. Path to NCover3 coverage report. See Notes on importing .NET reports below.

Test execution analysis parameters

Unless otherwise specified, these properties require values that are relative to project root.

All Languages

Analysis parameterDescription
sonar.testExecutionReportPathsComma-delimited list of paths to execution reports in the Generic Execution Data format. The paths must be relative to the project's base directory (root module).

C/C++/Objective-C

Analysis parameterDescription
sonar.cfamily.cppunit.reportsPathPath to the directory holding the CPPUnit reports. Note that while measures such as the number of tests are displayed at project level, no drilldown is available.

C#

Analysis parameterDescription
sonar.cs.vstest.reportsPathsPaths to VSTest reports. Multiple paths may be comma-delimited, or included via wildcards. Note that while measures such as the number of tests are displayed at project level, no drilldown is available.
sonar.cs.nunit.reportsPathsPaths to NUnit execution reports. Multiple paths may be comma-delimited, or included via wildcards. Note that while measures such as the number of tests are displayed at project level, no drilldown is available.
sonar.cs.xunit.reportsPathsPaths to xUnit execution reports. Multiple paths may be comma-delimited, or included via wildcards. Note that while measures such as the number of tests are displayed at project level, no drilldown is available.

Go

Analysis parameterDescription
sonar.go.tests.reportPathsComma-delimited list of paths to unit test report files. Paths may be absolute or relative to project root.

Java, Kotlin

Analysis parameterDescription
sonar.junit.reportPathsComma-delimited list of paths to Surefire XML-format reports.

Javascript and Typescript

Analysis parameterDescription
 You can use jest-sonar-reporter or karma-sonarqube-unit-reporter to create reports in the Generic Execution Data format. Both packages are available on npm.

PHP

Analysis parameterDescription
sonar.php.tests.reportPathPath to the PHPUnit unit test execution report file. Path may be absolute or relative to project root.

Python

Analysis parameterDescription
sonar.python.xunit.reportPathPath to unit test execution report. Leave unset to use the default (xunit-reports/xunit-result-*.xml). Path wildcards (see above) are supported. If any paths in the report are invalid, set sonar.python.xunit.skipDetails=true to collect only project-level details.

VB.NET

Analysis parameterDescription
sonar.vbnet.vstest.reportsPathsPaths to VSTest execution reports. Multiple paths may be comma-delimited, or included via wildcards. Note that while measures such as the number of tests are displayed at project level, no drilldown is available.
sonar.vbnet.nunit.reportsPathsPaths to NUnit execution reports. Multiple paths may be comma-delimited, or included via wildcards. Note that while measures such as the number of tests are displayed at project level, no drilldown is available.
sonar.vbnet.xunit.reportsPathsPaths to xUnit execution reports. Multiple paths may be comma-delimited, or included via wildcards. Note that while measures such as the number of tests are displayed at project level, no drilldown is available.

Importing .NET reports

To import .NET reports, the report generation process must be executed after the begin step and before the end MSBuild command. The following steps detail importing .NET reports:

  1. Run the SonarScanner.MSBuild.exe begin command, specifying the absolute path where the reports will be available using the /d:propertyKey="path" syntax ("propertyKey" depends on the tool)
  2. Build your project using MSBuild
  3. Run your test tool, instructing it to produce a report at the same location specified earlier to the MSBuild SonarQube Runner (How to generate reports with different tools)
  4. Run the SonarScanner.MSBuild.exe end command

For more information, see the Generate Reports for C#, VB.net Community Post.

Troubleshooting

Why doesn't coverage in SonarQube match my coverage tool?

Code coverage metrics may look different between SonarQube and the tool you've used to gather them. Often, this is because the two aren't using the same metrics.

External tools often use line coverage, calculated by dividing covered lines (lines to cover - uncovered lines) by the total number of executable lines (lines to cover).

SonarQube uses the covered lines from the imported coverage report and the executable lines (or lines to cover) to calculate its coverage metrics. SonarQube computes coverage as follows:

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)

SonarQube might also compute line coverage differently from your external tool. This is because the lines to cover may not be the same according to SonarQube and to the tool.

For more information on what SonarQube considers a line of code, see the Tests section on the metric-definitions page.