Skip to content

sonar.cxx.vscoveragexml.reportPaths

guwirth edited this page Mar 10, 2021 · 5 revisions

Overview

Sensor to read reports from the Visual Studio coverage tool. To determine what proportion of your project's code is actually being tested by coded tests such as unit tests, you can use the code coverage feature of Visual Studio. Code coverage analysis can be applied to both managed (CLI) and unmanaged (native) code.

Visual Studio provides block or line coverage:

  • block coverage: A block is a piece of code with exactly one entry and exit point. If the program’s control flow passes through a block during a test run, that block is counted as covered. The number of times the block is used has no effect on the result. The block coverage is only on function level available. Block coverage is not supported by SonarQube.
  • line coverage: Counts the number of executed lines of code. A line could be covered (covered="yes"), not covered (covered="no") or partial covered (covered="partial").

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

Supported versions

Create report

The command line tool CodeCoverage.exe is used for the coverage measurement in Visual Studio. This is located in Visual Studio 2019 Enterprise in the directory C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe. Not every version of Visual Studio includes the coverge tool. The CodeCoverage.exe runs as master of the unit test runner.

In order to run Visual Studio coverage and 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

Creating the coverage report for SonarQube is a two step process:

Generate a coverage file

CodeCoverage.exe collect /output:out.coverage [...]

where [...] is your test client.

Convert the resulting coverage file into an XML file

CodeCoverage.exe analyze /output:report.xml out.coverage

With this option it is also possible to merge several coverage results into one XML file.

CodeCoverage.exe analyze /output:result.xml out1.coverage out2.coverage

Example of a report file

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

<?xml version="1.0" encoding="UTF-8" ?>
<results>
  <modules>
    <module name="unittest1.dll" path="unittest1.dll" id="93AA4138F7AFB24C91DC9614B700B83C07000000" block_coverage="72.73" line_coverage="68.75" blocks_covered="8" blocks_not_covered="3" lines_covered="11" lines_partially_covered="1" lines_not_covered="4">
      <functions>
        <function id="77200" name="myFunc1" type_name="" block_coverage="62.50" line_coverage="61.54" blocks_covered="5" blocks_not_covered="3" lines_covered="8" lines_partially_covered="1" lines_not_covered="4">
          <ranges>
            <range source_id="0" covered="yes" start_line="4" start_column="0" end_line="4" end_column="0" />
            <range source_id="0" covered="yes" start_line="5" start_column="0" end_line="5" end_column="0" />
            <range source_id="0" covered="yes" start_line="6" start_column="0" end_line="6" end_column="0" />
            <range source_id="0" covered="yes" start_line="8" start_column="0" end_line="8" end_column="0" />
            <range source_id="0" covered="no" start_line="9" start_column="0" end_line="9" end_column="0" />
            <range source_id="0" covered="no" start_line="10" start_column="0" end_line="10" end_column="0" />
            <range source_id="0" covered="yes" start_line="13" start_column="0" end_line="13" end_column="0" />
            <range source_id="0" covered="yes" start_line="15" start_column="0" end_line="15" end_column="0" />
            <range source_id="0" covered="yes" start_line="16" start_column="0" end_line="16" end_column="0" />
            <range source_id="0" covered="partial" start_line="21" start_column="0" end_line="21" end_column="0" />
            <range source_id="0" covered="no" start_line="22" start_column="0" end_line="22" end_column="0" />
            <range source_id="0" covered="no" start_line="23" start_column="0" end_line="23" end_column="0" />
            <range source_id="0" covered="yes" start_line="25" start_column="0" end_line="25" end_column="0" />
          </ranges>
        </function>
      </functions>
      <source_files>
        <source_file id="0" path="x:\coveragetest\source1.cpp" checksum_type="MD5" checksum="8A4EC27BE0FE2BCE77401F836581836D">
        </source_file>
      </source_files>
    </module>
  </modules>
</results>

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.vscoveragexml.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.vscoveragexml.reportPaths=report.xml

Troubleshooting

Clone this wiki locally