Skip to content

sonar.cxx.cobertura.reportPaths

Günter Wirth edited this page Mar 28, 2022 · 8 revisions

Overview

Sensor to read Cobertura coverage reports. Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.

The Cobertura XML report contains the branch coverage and line coverage.

  • The line coverage can be mapped directly to the line coverage supported by SonarQube.
  • The SonarQube condition/decision coverage can be derived from the branch coverage.

Some C++ tools also generate coverage reports in Cobertura format, this includes, for example, gcovr and OpenCppCoverage.

Gcovr

gcovr provides a utility for managing the use of the GNU gcov utility and generating summarized code coverage results. The gcovr command can produce different kinds of coverage reports:

OpenCppCoverage

OpenCppCoverage is an open source code coverage tool for C++ under Windows. The tool supports compiler with program database file (.pdb). For use with this sensor --export_type=cobertura must be set.

Note: The cxx plugin itself does not run the tools, you have to do that yourself beforehand. The sensor only reads the report generated by the tools!

Supported versions

Create report

In order to generate a fitting report, make sure:

  • to call it from the projects root directory, so that the paths in the report fit
  • that the parameter matches the sonar.sources list in sonar-project.properties

gcovr sample

We compile example1.cpp with the GCC compiler as follows:

g++ -fprofile-arcs -ftest-coverage -fPIC -O0 example.cpp -o program

Note that we compile this program without optimization, because optimization may combine lines of code and otherwise change the flow of execution in the program. Additionally, we compile with the -fprofile-arcs -ftest-coverage -fPIC compiler options, which add logic to generate output files that can be processed by the gcov command.

The compiler generates the program executable. When we execute this command:

./program

the files example1.gcno and example1.gcda are generated. These files are processed with by gcov to generate code coverage statistics.

The gcovr command calls gcov and summarizes these code coverage statistics in various formats. The default output format for gcovr is to generate a tabular summary in plain text. The gcovr command can also generate an XML output using the --xml and --xml-pretty options. The XML output includes branch statistics and the number of times that each line was covered:

gcovr -r . --xml

OpenCppCoverage sample

In the root folder containing Sample.sln, open a new command line (cmd.exe or Microsoft Windows PowerShell) and execute the following command:

OpenCppCoverage --sources Sample --export_type=cobertura -- .\Debug\Sample.exe

OpenCppCoverage needs a little help to display only what you want. This is where the command line option --sources helps. This computes coverage only for the source files whose path contains "Sample". The result will be in Sample.xml.

Sometimes, it can be useful to have a global overview of the coverage for several test programs. However OpenCppCoverage allow you to run a single program at a time. You can use --export_type:binary to save a code coverage and merge it with other coverage reports.

OpenCppCoverage --sources=MySources --export_type=binary:Test1.cov -- Test1.exe
OpenCppCoverage --sources=MySources --export_type=binary:Test2.cov -- Test2.exe
OpenCppCoverage.exe --sources=MySources --input_coverage=Test1.cov --input_coverage=Test2.cov  -- Test3.exe

Example of a report file

If the tool was executed successfully, a report like the example below should be generated:

<?xml version="1.0" ?>
<!DOCTYPE coverage
SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-03.dtd'>
<coverage branch-rate="0.0" line-rate="0.27397260274" timestamp="1335184370" version="gcovr 2.5-prerelease (r2774)">
  <sources>
    <source>
      .
    </source>
  </sources>
  <packages>
    <package branch-rate="0.0" complexity="0.0" line-rate="0.0" name="sources.utils">
      <classes>
        <class branch-rate="0.0" complexity="0.0" filename="sources/file1.cpp" line-rate="0.0" name="file1_cpp">
          <lines>
            <line branch="false" hits="1" number="1"/>
            <line branch="false" hits="1" number="2"/>
            <line branch="false" hits="4" number="3"/>
            <line branch="false" hits="1" number="4"/>
          </lines>
        </class>
        <class branch-rate="0.0" complexity="0.0" filename="sources/file2.cpp" line-rate="0.0" name="file2_cpp">
          <lines>
            <line branch="false" hits="0" number="33"/>
            <line branch="true" condition-coverage="0% (0/2)" hits="0" number="35">
              <conditions>
              <condition coverage="0%" number="0" type="jump"/>
              </conditions>
            </line>
            <line branch="false" hits="0" number="36"/>
            <line branch="true" condition-coverage="0% (0/2)" hits="0" number="40">
              <conditions>
              <condition coverage="0%" number="0" type="jump"/>
              </conditions>
            </line>
          </lines>
        </class>
      </classes>
    </package>
    ...
  </packages>
</coverage>

Configure cxx plugin

  1. First check if the file extensions read in by the cxx plugin are set (sonar.cxx.file.suffixes).
  2. Set the analysis parameter sonar.cxx.cobertura.reportPaths in the configuration file sonar-project.properties of your project. The Report Paths link describes the configuration options.
  3. Execute the SonarScanner to transfer the project with the report to the SonarQube Server.

Sample for sonar-project.properties:

sonar.cxx.cobertura.reportPaths=coverage.xml

Troubleshooting

When the coverage is measured with gcov, it may be that many branches are not covered. By setting the options --exclude-unreachable-branches and --exclude-throw-branches an improvement can be achieved.

  • With the gcovr --exclude-unreachable-branches option, gcovr parses the source code to see whether that line even contains any code. If the line is empty or only contains curly braces, this could be an indication of compiler-generated code that was mis-attributed to that line (such as that for static destruction) and branch coverage will be ignored on that line.
  • With the gcovr --exclude-throw-branches option, exception-only branches will be ignored. These are typically arcs from a function call into an exception handler.
Clone this wiki locally