Exporting code coverage results in SonarQube format

SonarQube is a format that can be imported into a SonarQube server.

Procedure

  1. In the Code Coverage Results view, select the result that you want to export, right-click and select Export.
    The Coverage Result Export window opens. When you select multiple results, the results are merged and then exported as a single result.
  2. From the Export Format drop-down list, select SonarQube.
    To export the result in a different encoding than the default UTF-8, click Settings to specify the encoding.
  3. Specify an empty result location:
    • To export to a local directory, select Local from the Connection list. In the Location field, type the file path or use Browse to select a file.
    • To export to a remote location, select an existing remote location from the Connection list or use New to create one. In the Location field, type the remote directory or use Browse to select a directory from the remote location.
    The Coverage Result Export window will remember the destination that you specify after you complete exporting.
  4. Enter the result file name or use the default name.
    Note: Specify the name of the result file, or leave the field blank to stay with the default name. The file name cannot contain special characters. In addition, avoid using the forward slash (/).
    The file extension will be automatically added based on the format that you select for exporting the results.
  5. Click Finish to export the results.

    If the selected results contain files that have the same name but different content, the merged result report might be inaccurate. A warning message is displayed for you to confirm merging the results or cancel the request.

Results

When the result exporting is done, you can find the exported files in SonarQube format in the result location that you specified.

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.
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.