[Coverage & Test Data] Generate Reports for Swift

Here is a quick guide on importing your Coverage or Test Data reports into SonarQube or SonarCloud. These commands were used when we implemented this feature. If you use other commands, feel free to share them as replies.
If you have trouble importing your Coverage or Test data reports, please ask for help on the corresponding SonarQube or SonarCloud category.

Rely on the “Generic Test Data”

SonarQube supports a generic coverage format, allowing you to load any coverage report as soon as you can convert a native coverage format to the SonarQube’s format.

Using Sonar-provided script

Xcode 13.3 or later is required.

Xcode provides a tool xccov that lets you obtain coverage data for all your files. You can then convert this data into the SonarQube generic coverage format and load it thanks to the property sonar.coverageReportPaths (see documentation).

As described here, you can use the script xccov-to-sonarqube-generic.sh from the Sonar scanning examples. Use it as below:

bash xccov-to-sonarqube-generic.sh path/to/test.xcresult/ >sonarqube-generic-coverage.xml

Run analysis with sonar.coverageReportPaths property.

sonar-scanner -Dsonar.coverageReportPaths=sonarqube-generic-coverage.xml

The xccov-to-sonarqube-generic.sh script uses the following xccov command to extract the coverage data:

xcrun xccov view --archive "path/to/test.xcresult"

Using Slather

You can also use the opensource tool Slather that will help you generate the SonarQube generic test data report using the --sonarqube-xml option as in the example below:

slather coverage --sonarqube-xml --scheme <schemeName> --build-directory <buildDirectory>  path/to/project.xcodeproj

Please, refer to the Slather documentation for instructions on how to use it.

Using sonar.swift.coverage.reportPaths (llvm-cov based)

As described here, you can also import code coverage using the llvm-cov format.

  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=<pathToCoverageProfdata> <pathToInstrumentedExecutable> > <coverageReportFile>
  1. Import this report while running the SonarQube analysis by providing the path to the coverage report through the sonar.swift.coverage.reportPaths property. Note that coverage report generation and SonarQube analysis should be performed on the same machine since coverage reports contain absolute paths.

A sample project can be downloaded here: sonar-scanning-examples/swift-coverage at master · SonarSource/sonar-scanning-examples · GitHub


This content was previously part of SonarQube’s documentation, but it applies to both SonarQube and SonarCloud, so we thought it would be better to share it here.

A post was split to a new topic: Problem when generating coverage report for Swift

A post was split to a new topic: Swift coverage problem with XCode 10

A post was split to a new topic: Covert Salesforce Apex’s Test Code Coverage JSON to generic coverage