Exporting code coverage results in SonarQube format

You can export code coverage results in SonarQube format.

Procedure

  1. In the Code Coverage Results view, select the file that you want to export, right-click and select Export.
    When you select multiple files, the results are merged and then exported as a single file.
  2. In the Coverage Result Export dialog box that opens, select SonarQube from the Export Format drop-down list.
  3. Specify the Destination folder that you want to store the exported files.
    Note: The destination folder must be a valid path. If you specify an invalid path, you will see a warning in the view. The Coverage Result Export wizard will remember the destination folder that you specify so you do not need to specify it again next time.
  4. Enter the result file name or use the default name.
    Note:
    The file extension will be automatically added based on the format that you select for exporting the results.
  5. Optional: If the Settings button is enabled, you can select to further change the exporter’s behaviour.
  6. Optional: Select the Add Location to Code Coverage Results View checkbox to add the export directory as a result location to the Code Coverage Results view.
  7. Click Finish to export the results.

Results

When the result exporting is done, you can find the exported files in SonarQube format in the destination folder that you specify.

The exported files in SonarQube format include a .xml file of coverage report, a .properties file that contains SonarQube Scanner settings, and the source code that matches the report. For example, you can find a typical output folder structure for the exported results in SonarQube format as below.
Root/coverageReport.xml
    /sonar-project.properties
    /src/...     
  • coverageReport.xml is an xml file in generic SonarQube coverage report format, which contains information about covered lines. The file name can be chosen arbitrarily, as one of the arguments for exportResult method.
  • sonar-project.properties is the configuration file for SonarQube Scanner. CCSonarQubeExporter prints the default and user-defined properties that are required to this file to import the coverage data into SonarQube:
    • As one of the parameters to exportResult method, an instance of ICCExporterSettings contains methods setProperty and getProperty. Most of the SonarQube properties have the prefix sonar. and the exporter writes any properties that have this prefix into the file.
    • Certain properties have their reasonable default value, or the value can be guessed with the other given data that is provided to the exporter. For example, sonar.sources equals to the src folder and sonar.coverageReportPaths equals to the coverage report file name. These key-value pairs appear in the file as well.
    • Some properties are required by SonarQube, such as sonar.projectKey. If no value is provided in the ICCExporterSettings, the exporter still prints out such keys with no value but the lines are commented out as follows:
      #sonar.projectKey=
      You can edit the file later to define any missing properties or to provide these values as command-line arguments for SonarQube Scanner.
  • src is the folder that contains source files, listings, or both.