[iOS] Code covergage with Sonarcloud

Hello, everybody!

My team just added our project’s repo to Sonarcloud, it succesfully scanned it automatically. We are currently receiving the reports both from scanning the repository globally and in every PR. However, we’re not getting data for code coverage in neither case.

On the other hand we’re using Bitrise as our CI tool, in one of our many steps we’re generating our tests report via fastlane with xcov. Is there a way to display our code coverage in the sonarcloud reports via our CI tool?

We tried adding both run-sonar-swift.sh scripts and sonar-project.properties but were unsuccessful.

If there’s any way you can help us or if there’s any info we could’ve missed,
we’d really appreciate it!

  • ALM used: GitHub
  • CI system used: Bitrise
  • Scanner command used when applicable (private details masked)
  • Languages of the repository: Swift
1 Like

Hey there.

Have you checked the guide here?

https://community.sonarsource.com/t/coverage-test-data-generate-reports-for-swift/9700/6

1 Like

Thank your very much for responding Colin!

We actually came across that thread in one of our many attempts
at publishing test coverage in Sonarcloud. What we couldn’t figure out with that approach is the following:

  1. If sonar cloud runs our unit tests in which folder path are they stored? For example, when we run our tests locally the results of that build are stored in DerivedData.
  2. In the root folder of our project we have a fastfile for our CI tool. In one of those many lane steps, we generate a code coverage report through xcov (xcov - fastlane docs) and the tests results are stored in a separate folder (attached photo). We thought that maybe Fastlane is colliding with Sonarcloud’s scanner, but we wanted to confirm this with you guys.

Thank you!

Hey there.

I understand a bit better now – it seems you’re using Automatic Analysis which, as documented, doesn’t support the import of coverage reports.

  • Code coverage information is not supported.

You’ll need to switch to CI-based analysis if you want to import coverage reports. Since Bitrise isn’t supported out of the box, this will require some additional configuration for Pull Request analysis (only running the SonarScanner with sonar.pullrequest.* parameters when a PR build is being run, and deriving the values from environment variables.

When running CI-based analysis, you should be able to import coverage reports for your Swift code.

3 Likes

Thank you very much for your answer, Colin.
We’ll definitely give it a try and let you know!

1 Like

Hey Colin.

We successfully implemented the manual configuration you suggested. These are our steps:

  • We created in the root directory of our project both sonar-project.properties and xccov-to-sonarqube-generic.sh script (since we use XCode 13.2.x, we followed the suggestions mentioned in this pull request).

  • Bitrise steps (relevant ones): fastlane’s scan, install jq (Homebrew), xccov-to-sonarqube-generic.sh script, SonarQube scanner.

This worked fine, the sonarqube-generic-coverage.xml was generated and we could publish our code coverage data to Sonarcloud. However, we discovered that running the generic data script takes a quite a while both when we run it locally and in our CI.

Do you know by any chance, or have additional info of a way to improve this?

Thank you for your help!

1 Like

Hello, again!

Following the previous post, we tried a different approach to create our generic data xml file. Since we use Fastlane we added a slather lane (both in our Fastfile and as a step in Bitrise). In one of its parameters you can output a sonarqube-generic-coverage.xml file (and quite fast I might add).

Unfortunately, even though Sonar Scanner recognizes the generated file, it cannot read its content returning 0.0% in coverage. The only difference we could find was that the paths to the files that have coverage data are different: slather generates relative paths and the xccov-to-sonarqube-generic.sh script absolute paths.

My team agrees that this could be a great option to generate de required file for test coverage.

So once again, if you guys have more information or know of a way to get this file’s correctly we’d love to know!

Thank you very much!

1 Like