https://github.com/dfed/swift-testing-expectation
Create an asynchronous expectation in Swift Testing
https://github.com/dfed/swift-testing-expectation
Last synced: 7 months ago
JSON representation
Create an asynchronous expectation in Swift Testing
- Host: GitHub
- URL: https://github.com/dfed/swift-testing-expectation
- Owner: dfed
- License: mit
- Created: 2024-10-20T02:14:36.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-23T18:54:35.000Z (9 months ago)
- Last Synced: 2025-04-16T05:17:49.201Z (7 months ago)
- Language: Swift
- Homepage:
- Size: 41 KB
- Stars: 51
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: Contributing.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-swift - swift-testing-expectation - Create an asynchronous expectation in Swift Testing. (Libs / Testing)
- fucking-awesome-swift - swift-testing-expectation - Create an asynchronous expectation in Swift Testing. (Libs / Testing)
README
# swift-testing-expectation
[](https://github.com/dfed/swift-testing-expectation/actions?query=workflow%3ACI+branch%3Amain)
[](https://codecov.io/gh/dfed/swift-testing-expectation)
[](https://spdx.org/licenses/MIT.html)
[](https://swiftpackageindex.com/dfed/swift-testing-expectation)
[](https://swiftpackageindex.com/dfed/swift-testing-expectation)
Create an asynchronous expectation in Swift Testing
## Testing with asynchronous expectations
The [Swift Testing](https://developer.apple.com/documentation/testing/testing-asynchronous-code) framework vends a [confirmation](https://developer.apple.com/documentation/testing/confirmation(_:expectedcount:isolation:sourcelocation:_:)) method which enables testing asynchronous code. However unlike [XCTest](https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations)’s [XCTestExpectation](https://developer.apple.com/documentation/xctest/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:
```swift
@Test func testMethodEventuallyTriggersClosure() async {
let expectation = Expectation()
systemUnderTest.closure = { expectation.fulfill() }
systemUnderTest.method()
await expectation.fulfillment(within: .seconds(5))
}
```
### Waiting for multiple expectations
The `Expectations` type vended from this library makes it easy to wait for multiple expectations:
```swift
@Test func testMethodEventuallyTriggersClosures() async {
let expectation1 = Expectation()
let expectation2 = Expectation()
let expectation3 = Expectation()
systemUnderTest.closure1 = { expectation1.fulfill() }
systemUnderTest.closure2 = { expectation2.fulfill() }
systemUnderTest.closure3 = { expectation3.fulfill() }
systemUnderTest.method()
await Expectations(expectation1, expectation2, expectation3).fulfillment(within: .seconds(5))
}
```
## Installation
### Swift Package Manager
To install swift-testing-expectation in your project with [Swift Package Manager](https://github.com/apple/swift-package-manager), the following lines can be added to your `Package.swift` file:
```swift
dependencies: [
.package(url: "https://github.com/dfed/swift-testing-expectation", from: "0.1.0"),
]
```
### CocoaPods
To install swift-testing-expectation in your project with [CocoaPods](http://cocoapods.org), add the following to your `Podfile`:
```
pod 'TestingExpectation', '~> 0.1.0'
```
## Contributing
I’m glad you’re interested in swift-testing-expectation, and I’d love to see where you take it. Please read the [contributing guidelines](Contributing.md) prior to submitting a Pull Request.