SonarCloud Showing 0% Code Coverage for Swift [Xcode 13]

I have been trying this for a couple of days to get the code coverage on the sonar cloud, but the sonar cloud is not able to detect the test cases and shows 0% code coverage.

Specification: Xcode version: 13.2.1, Mac OS X 12.2.1

So, far I have tried as follow

  1. Followed this link sonar-scanning-examples/swift-coverage at master · SonarSource/sonar-scanning-examples · GitHub.
    1. Build the Project
    2. Creat code coverage report.
      but in 3rd step importing the code coverage report I am getting the following error:
INFO: Scanner configuration file: /usr/local/Cellar/sonar-scanner/
INFO: Project root configuration file: NONE
INFO: SonarScanner
INFO: Java 11.0.12 Homebrew (64-bit)
INFO: Mac OS X 12.2.1 x86_64
INFO: User cache: /Users/admin/.sonar/cache
ERROR: SonarQube server [http://localhost:9000] can not be reached
INFO: ------------------------------------------------------------------------
INFO: ------------------------------------------------------------------------
INFO: Total time: 0.769s
INFO: Final Memory: 3M/17M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarScanner execution

Apart from this, I have tried giving code coverage path in Adminstrater> Language> Code Coverage Report.

but nothing is working for me, anyone can please look into this issue.

Thank You.

Hey there.

It looks like the error you’re receiving doesn’t have anything to do with code coverage – but that your scan is running without being pointed to and probably without the required sonar.organization and sonar.projectKey configuration.

Presumably, you have an analysis somewhere that is already successfully analyzing (and where 0% code coverage is being reported). When you’re running your regular project (not the example), where are you running it? In a particular CI (GitHub actions, Bitbucket Pipelines, etc.)

1 Like

Hi Colin,

We are using the Bitbucket pipeline and I have added the above keys in my .yml pipeline file.

find command below.

  • export CODE_COVERAGE=./sonarqube-generic-coverage.xml
    - sonar-scanner -Dsonar.projectKey=projKey -Dsonar.organization=OrgKey -Dsonar.sources=. -Dsonar.coverageReportPaths=$CODE_COVERAGE

but still, sonar cloud is showing 0.0% Code Coverage.

Can you post your full bitbucket-pipelines.yml file?

please find the same below:

image: gcc:9.1.0

  depth: full              # SonarCloud scanner needs the full history to assign issues properly

    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarCloud
          - sonar
          - docker
          - export SONAR_SCANNER_VERSION=
          - export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-$SONAR_SCANNER_VERSION-linux
          - curl --create-dirs -sSLo $HOME/.sonar/$
          - unzip -o $HOME/.sonar/ -d $HOME/.sonar/
          - export PATH=$SONAR_SCANNER_HOME/bin:$PATH
          - export COMPILE_DATABASE=./compile_commands.json
          - export CODE_COVERAGE=./sonarqube-generic-coverage.xml
          - sonar-scanner -Dsonar.projectKey=proj-key -Dsonar.organization=organization-key -Dsonar.sources=. -Dsonar.coverageReportPaths=$CODE_COVERAGE

pipelines:                 # More info here:
      - step: *build-test-sonarcloud
      - step: *build-test-sonarcloud

any update??? @Colin

Hey there.

Please do not bump threads – and certainly not when your last update was mid-day on a Friday. While we are happy to provide free support to our users, please be respectful.

The bitbucket-pipelines.yml file doesn’t actually show how you produce the code coverage report (which to be clear should be generated each time, and not stored in your repository).

The code coverage should also be produced on the same machine where analysis takes place (so running analysis on a linux gcc container when coverage was produced on a macOS machine won’t work).

@Akhil_Verma Coming back to this, I am guessing you are trying to do it this way because of some limitation on Bitbucket regarding macOS runners? Where are you usually building this code?

Hi @Colin

I am building my code on my local device(MacBook).

After successfully testing my code on Xcode the process we are following is as follows:

  1. I am building the project on my local machine with
    xcodebuild -project swift-coverage-example.xcodeproj/ -scheme swift-coverage-example -derivedDataPath Build/ -enableCodeCoverage YES clean build test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO

  2. Creating a code coverage report using:
    bash Build/Logs/Test/*.xcresult/ > sonarqube-generic-coverage.xml

and the script I am using you can find here

** This command will generate a generic code coverage report.

  1. Then committing the changes to BitBucket.

PFA bitbucket-pipeline.yml file and generic code coverage file, (555 Bytes) (1.5 KB)