I'm wondering if the tagging functionality which was added for Swift testing will be added to XCTest for UITests anytime soon.
Swift Testing
RSS for tagSwift Testing is a framework with expressive and intuitive APIs that make testing your Swift code a breeze.
Posts under Swift Testing tag
33 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
Hi!
I have some unit tests which send and receive local notifications. Unfortunately, when ran on Xcode Cloud, the tests time out. I assume due to a call to requestAuthorization in init() (I have started using Swift Testing).
Is there a way to get around this issue in order to run the tests?
For my app I was trying to write some tests to ensure the functionality of all features. As I am using Xcode 16.0 I thought I might use Swift testing which was newly introduced and replaces XCTest.
I created a new test target with Swift Testing and tried to run the first test, which was created automatically by the system.
struct FinancialTests {
@Test func testExample() async throws {
#expect(true)
}
}
Xcode is also showing the test diamond next to the function so I clicked on it to execute it. The app started to build and the build ended successfully. The the next step was testing. And after waiting for 10 minutes or so, no test was executed. First I thought maybe the test was not found, but in the test case overview all tests were shown:
The run only shows this:
Can someone help me to get this running.
Many thanks!
Hello!
When running tests on iOS 17, the build succeeds but does not run on iOS 16. It consistently gets stuck on 'Testing'.
This issue does not occur on iOS 17.
I'm used to wait for expectations when using XCTest, but I'm completely stumped by the seemingly-absent option for expecting some parts of my code to be called in Swift Testing 🤔
Consider the following:
protocol MyAPI {
func getData() async throws -> String
}
class MockAPI: MyAPI {
let stub: () async throws -> String
init(stub: @escaping () async throws -> String = { "hello" }) {
self.stub = stub
}
func getData() async throws -> String {
try await stub()
}
}
Which is being used in my view model:
class MyViewModel: ObservableObject {
private let api: MyAPI
@Published
var data: String
init(api: MyAPI) {
self.api = api
}
func refresh() async throws {
self.data = try await api.getData()
}
}
When I wrote tests in the past, I would simply override the stub of the MockAPI implementation and fulfill an expectation in there:
class MyViewModelTests: XCTestCase {
func testModelCallsAPIOnRefresh() async throws {
let expectCallsAPI = expectation("Model should call API")
let api = MockAPI {
expectCallsAPI.fulfill()
return "hello"
}
let model = MyViewModel(api: api)
try await model.refresh()
await fulfillment(of: [expectCallsAPI], timeout: 1)
}
}
How would I go about checking that my model does indeed call the API when using Swift Testing?
Running Xcode 16b4 on Apple silicon.
Create a fresh Swift package, it will be using swift-tools-version: 6.0. Add a Swift Test to the test target with some basic assertion like #expect(true).
Run the test against the target My Mac and the test will compile, run and pass.
Run the test against the target My Mac (Mac Catalyst) and the test will compile and crash with EXC_BAD_ACCESS.
If you create an equivalent XCTest, it will compile, run and pass against both destinations.
Has anyone else experienced this? Is this already being tracked?
Is this possibly related? https://github.com/swiftlang/swift/pull/75432
With XCTest you were able to tun tests with:
MyTestCase.defaultTestSuite.run()
How can we use the new testing framework with Swift Playgrounds?
Does Swift Testing support using ‘any’ values as parameters like seen here for ‘any ChartDataPoint.Type’
@Test(arguments: [
(expectedBodyFatValues,
HKQuantityTypeIdentifier.bodyFatPercentage,
HKUnit.percent(),
BodyFatPoint.self),
(expectedActiveEnergyValues,
HKQuantityTypeIdentifier.activeEnergyBurned,
HKUnit.kilocalorie(),
ActiveCaloriesPoint.self),
(expectedBodyMassValues,
HKQuantityTypeIdentifier.bodyMass,
HKUnit.pound(),
BodyMassPoint.self),
(expectedBasalEnergyValues,
HKQuantityTypeIdentifier.basalEnergyBurned,
HKUnit.kilocalorie(),
BasalEnergyPoint.self)
])
func healthKitDataReading(
expectedData: [Double],
identifier: HKQuantityTypeIdentifier,
unit: HKUnit,
dataChartType: any ChartDataPoint.Type
) async throws {...}
Currently I can’t get this code to work, and see the error
… Conflicting arguments to generic parameter 'C' ('[([Double], HKQuantityTypeIdentifier, HKUnit, BodyFatPoint.Type)]' vs. '[([Double], HKQuantityTypeIdentifier, HKUnit, ActiveCaloriesPoint.Type)]' vs. '[([Double], HKQuantityTypeIdentifier, HKUnit, BodyMassPoint.Type)]' vs. '[([Double], HKQuantityTypeIdentifier, HKUnit, BasalEnergyPoint.Type)]')
Also, I can’t seem to use variables like ‘expectedBodyFatValues’ due to the error
Instance member 'expectedBodyFatValues' cannot be used on type 'Health_Mix_Swift_Tests'; did you mean to use a value of this type instead?
Only way I’ve found around this is including the entire array of values as the parameter, but it’s very cumbersome.
Why doesn’t deinit support async? At the end of a test, I want to wipe data from HealthKit, and it’s delete function is asynchronous.
From the Documentation of .timeLimit(_:):
Test timeouts do not support high-precision, arbitrarily short durations due to variability in testing environments. The time limit must be at least one minute, and can only be expressed in increments of one minute.
Unit Tests are usually considered too long when they take more than 0.1 seconds. So a minimal time limit of on minute is completely useless for unit tests.
Is there a similar trait in Swift Testing to handle millisecond time limits?
Hello everyone,
I’m encountering an issue with my Swift Testing suite where state changes made in one test method do not persist to another. I am using Swift’s @Suite and @Test annotations to group and serialize my tests, but it seems that the state is not being carried over between the tests. The second function fails with:
Expectation failed: (string → nil) != nil
Here is my example code:
import Testing
@Suite(.serialized)
struct createCheckTests {
var value = 25
var string: String? = nil
@Test("Create string")
mutating func stringCreation() {
#expect(value > 0)
string = "Value is: \(value)"
}
@Test("Check string")
func stringCheck() {
#expect(string != nil, "The string is nil")
print("\(String(describing: string))")
}
}
What is the correct way to approach such a scenario where I want to test two functions that are related, one to generate some value and one to check that generated value against it initial value using Suites to group and isolate them from other tests?
Thanks.
Is it a known issue that when attempting to test SwiftData objects with relationships in Swift Testing there are errors that do not occur when the app is running?
Example:
@Relationship(deleteRule: .cascade, inverse: \MBAccount.book) var accounts: [MBAccount] = []
and
@Relationship var book: Microbook?
Then the test:
let book = Microbook(name: "Julia's Cupcakes")
let account1 = MBAccount(name: "Chase Business Account")
let account2 = MBAccount(name: "Cash Drawer")
account1.book = book
account2.book = book
#expect(account1.name == "Chase Business Account")
#expect(account2.type == "asset")
}
Produces a fatal error while running test:
SwiftData/PersistentModel.swift:321: Fatal error: Unable to find relationship on MBAccount for KeyPath \MBAccount.book
Any ideas?
Currently Swift Testing has much less features than XCTest, so the adoption will be very slow from our side. Notable features we miss are UI tests, performance tests and attachments.
I did not want to create many issues in Swift Testing GitHub project as lots of these shortcoming are most probably tracked internally (I can see lots of references to radars in GitHub issues.)
So, my question is: Is it a good idea to wait with wider adoption or should we experiment with other tools like swift Benchmarks?