Here is a quick guide on how to import 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 to import your Coverage or Test data reports, please ask for help on the dedicated Get help category.
For Xcode 9.3+ : Rely on the “Generic Test Data”
SonarQube supports a generic coverage format allowing you 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 xccov-to-sonarqube-generic.sh script from the sonar-scanning-examples/swift-coverage to create the “Generic Test Data” report.
XCode version | Command |
---|---|
XCode 9.3 - 9.4.1 | bash xccov-to-sonarqube-generic.sh Build/Logs/Test/*.xccovarchive/ > sonarqube-generic-coverage.xml |
XCode 10 | bash xccov-to-sonarqube-generic.sh Build/Logs/Test/*.xcresult/*_Test/*.xccovarchive/ > sonarqube-generic-coverage.xml |
XCode 11 | bash xccov-to-sonarqube-generic.sh Build/Logs/Test/*.xcresult/ > sonarqube-generic-coverage.xml |
Run analysis with sonar.coverageReportPaths
property.
sonar-scanner -Dsonar.projectKey=TestCoverage -Dsonar.sources=. -Dsonar.coverageReportPaths=sonarqube-generic-coverage.xml
The xccov-to-sonarqube-generic.sh 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
- 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>
- 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.
Property | Description |
---|---|
sonar.swift.coverage.reportPath |
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: https://github.com/SonarSource/sonar-scanning-examples/tree/master/swift-coverage
This content was previously part of SonarQube’s documentation but it applies for both SonarQube and SonarCloud so we though it would be better to share it here.