Skip to content

dfed/swift-testing-expectation

Repository files navigation

swift-testing-expectation

CI Status codecov License

Making it easy to create an asynchronous expectation in Swift Testing

Testing with asynchronous expectations

The Swift Testing vends a confirmation method which enables testing asynchronous code. However unlike XCTest’s XCTestExpectation, this confirmation must be confirmed before the confirmation’s body completes. Swift Testing has no out-of-the-box way to ensure that an expectation is fulfilled at some indeterminate point in the future.

The Expectation vended from this library fills that gap:

@Test func testMethodCallEventuallyTriggersClosure() async {
	let expectation = Expectation()

	systemUnderTest.closure = { expectation.fulfill() }
	systemUnderTest.method()

	await expectation.fulfillment(within: .seconds(5))
}

Requirements

  • Xcode 16.0 or later.
  • iOS 16 or later.
  • tvOS 16 or later.
  • watchOS 9 or later.
  • macOS 13 or later.
  • Swift 5.10 or later.

Installation

Swift Package Manager

To install swift-async-queue in your project with Swift Package Manager, the following lines can be added to your Package.swift file:

dependencies: [
	.package(url: "https://github.com/dfed/swift-testing-expectation", from: "0.1.0"),
]