Testing async code with Swift Concurrency

What's New

Release 0.0.7


Removes throws from waitForExpectations.


Xcode includes XCTest which only has waitForExpectations(timeout:handler:) to support async tests. It does not support waiting on a given list of expectations which is necessary for more complex async activity. The original wait(for:timeout:enforceOrder:) function does take a list of expectations but is not compatible with modern Swift Concurrency.

This package extends XCTestCase to include the asyncExpectation and waitForExpectations functions shown below which mimic existing functions to create and wait on expectations. See the unit tests for more reference code.

let done = asyncExpectation(description: "done")
Task {
    try await Task.sleep(seconds: 0.1)
    await done.fulfill()
try await waitForExpectations([done])

Alternatively, the AsyncTesting type can be used to access the same behavior with static functions which may clearly distinguish these functions from native XCTest functions.


  • Swift Tools 5.6.0
View More Packages from this Author


  • None
Last updated: Sun Nov 13 2022 11:42:29 GMT-0500 (GMT-05:00)