Skip to end of metadata
Go to start of metadata


SonarSwift is able to import coverage reports generated by Xcode using two approaches: relying on the Generic Test Data format or based on .profdata file.

For Xcode 9.3+ : Rely on the "Generic Test Data"

SonarQube supports a generic coverage format allowing to load any coverage report as soon as you find a way to convert a native coverage format to the SonarQube's format.

Xcode 9.3 introduced a tool called xccov that lets you obtain coverage data per file. You can then convert this data into the SonarQube Generic format, and load it thanks to the property sonar.coverageReportPaths (see documentation).

You can use the script from the sonar-scanning-examples/swift-coverage to outputs the "Generic Test Data" report.

bash Build/Logs/Test/*.xccovarchive/ > sonarqube-generic-coverage.xml

sonar-scanner -Dsonar.projectKey=TestCoverage -Dsonar.sources=. -Dsonar.coverageReportPaths=sonarqube-generic-coverage.xml 

The script uses the following xccov commands to convert the Xcode format into SonarQube's format:

  • list all the files potentially having coverage data:

    xcrun xccov view --file-list "$xccovarchive_file"
  • for each file, get the coverage details:

    xcrun xccov view --file "$file_name" "$xccovarchive_file"

For Xcode 7 - 9.2 : Rely on .profdata

  1. Prior to the SonarQube analysis, execute your unit tests and generate the coverage report.
    Note that the scheme you use to build the project should have coverage enabled. The "xcodebuild" command for your project might require more parameters than in the example below.

    xcodebuild -scheme <schemeName> -enableCodeCoverage YES -derivedDataPath . clean build test
    xcrun llvm-cov show -instr-profile=Build/Intermediates/CodeCoverage/Coverage.profdata <path to instrumented executable>   >   <coverage report file>
  2. Import this report while running the SonarQube analysis by providing the path to the coverage report through the following property. Note that coverage report generation and SonarQube analysis should be performed on the same machine, since coverage reports contain absolute paths.  



    Path of the coverage report generated from \"llvm-cov show\". This path can be either absolute or relative to the project directory.

A sample project can be downloaded here:


  • No labels