Tests files seem to be treated as source files in SonarCloud analysis?

Hello,

I’m learning how to use Sonar for a Swift test project with Github Actions.

This very simple project consists in a framework containing a TestCIModel folder with a swift file, containing a basic model. At the root of this project is also contained a TestCIModelTests folder containing the corresponding test files.

I’ve also added a very simple sonar-project.properties file at the root of the project:

sonar.organization={organization}
sonar.projectKey={project_key}

sonar.sources=TestCIModel
sonar.tests=TestCIModelTests

And about the Github Action, I just followed the official instructions:

on:
  pull_request:

name: Pull Request

jobs:
  sonarcloud:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
    - name: SonarCloud Scan
      uses: sonarsource/sonarcloud-github-action@v2.3.0
      env:
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

Everything seems to run fine, but in the analysis, it looks like the test file is treated as a source file:

Resulting in a 0% code coverage:

image

Is there something wrong, or did I forget something about how to setup the project?

Also, not totally related, but although I’ve this 0% code coverage issue, this doesn’t seem to prevent the quality gate from passing:

Capture d’écran 2024-08-02 à 15.13.53

Is there a specific config to set for this?

Thank you for your help!

Hi @Robtles ,
I guess you expect to see some coverage in the server. Setting up the test files is not enough. Identifying the tests allows you to apply specific rules to the test files.
To show the coverage in the report, you must run unit tests, generate a coverage report and set the path. Test coverage overview
For the quality gate that passes with 0% coverage, there is a paramete rsettingsonar.qualitygate.ignoreSmallChanges that allows to ignore quality gate for “small changes”. By default less than 20 lines.

2 Likes

Hi @Bachri_Abdel and thank you very much for your explanation!
I just came across this tool, slather, which seems to do what’s expected from sonar. I don’t know if there is a recommended way to do this for Swift projects, I’ll have a look at it.

Thank you again!

Hi @Robtles ,
You will find excellent article from @ganncamp to import Swift coverage here : [Coverage & Test Data] Generate Reports for Swift

1 Like

Hi @Bachri_Abdel and thank you again for your help!

FYI I ended up using xccov rather than slather. The topic you provided was really helpful, as well as this README.

Thanks again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.