I have created a very simple test project to try to implement SonarCloud in the Github Actions of my Swift project.
The project root contains a TestCIModel
folder containing the sources, and a TestCIModelTests
folder containing the tests.
Model.swift
inside the sources folder:
public struct Character {
public var name: String
public var age: Int
public init(name: String, age: Int) {
self.name = name
self.age = age
}
public mutating func update(name: String) {
self.name = name
}
}
TestCIModelTests.swift
inside the tests folder:
import XCTest
@testable import TestCIModel
final class TestCIModelTests: XCTestCase {
var character: Character?
func testCharacterInitialization() {
let testName = "john"
let testAge = 10
character = .init(
name: testName,
age: testAge
)
XCTAssertNotNil(character)
XCTAssertEqual(character?.name, testName)
XCTAssertEqual(character?.age, testAge)
}
}
I’ve also set a sonar-project.properties
file at the root of my project:
sonar.organization={organization}
sonar.projectKey={project_key}
sonar.sources=TestCIModel
sonar.tests=TestCIModelTests
And I’ve added a Github action to start a SonarCloud analysis on each pull request:
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/[email protected]
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
Everything seems to run fine… but in SonarCloud’s analysis, it looks like the test file is treated like a source file:
Resulting in a 0% code coverage result:
What am I doing wrong?
Thank you for your help!