Example code coverage setup for Swift and GitHub Actions

  • ALM / CI: GitHub+Actions
  • Organization configured cloud integration
  • Swift

We have a project with a working SonarCloud integration. I would like to improve it by adding code coverage. Are there examples available for how to setup code coverage? Also, I don’t have access to the configuration of SonarCloud in my GitHub project. Is that necessary to get things done in the right order?

Hi,

Welcome to the community!

Are you running analysis in your CI, or are you using automatic analysis? If the latter, you’ll have to start by deactivating it. Then you’ll need to configure analysis in your CI, and add the test coverage in that configuration.

 
HTH,
Ann

1 Like

Thank you for those pointers!

1 Like

I got it working by extending my existing workflow with

  - name: Install Sonar Scanner
    run: brew install sonar-scanner
...
  - name: Build
    run: set -o pipefail && xcodebuild build-for-testing -scheme "$scheme" -project "$project" -destination "$destination" -derivedDataPath Build/ -enableCodeCoverage YES | xcbeautify --renderer github-actions
  - name: Test
    run: set -o pipefail && xcodebuild test-without-building -scheme "$scheme" -project "$project" -destination "$destination" -derivedDataPath Build/ -enableCodeCoverage YES | xcbeautify --renderer github-actions
  - name: Convert coverage report to SonarCloud format
    run: |
       chmod +x ./xccov-to-sonarqube-generic.sh
       ./xccov-to-sonarqube-generic.sh Build/Logs/Test/*.xcresult/ > sonarqube-generic-coverage.xml
  - name: Generate and upload code analysis report
    run: |  
      sonar-scanner \
      -Dsonar.projectKey=<REDACTED> \
      -Dsonar.organization=<REDACTED> \
      -Dsonar.host.url=https://sonarcloud.io \
      -Dsonar.login=${{ secrets.SONAR_TOKEN }} \
      -Dsonar.sources=. \
      -Dsonar.cfamily.build-wrapper-output.bypass=true \
      -Dsonar.coverageReportPaths=sonarqube-generic-coverage.xml \
      -Dsonar.c.file.suffixes=- \
      -Dsonar.cpp.file.suffixes=- \
      -Dsonar.objc.file.suffixes=- \
1 Like