Skip to content

Instantly share code, notes, and snippets.

@kirilltitov
Created March 3, 2021 10:36
Show Gist options
  • Save kirilltitov/4600db18d64a9fc0ebc6a6939aa9ab98 to your computer and use it in GitHub Desktop.
Save kirilltitov/4600db18d64a9fc0ebc6a6939aa9ab98 to your computer and use it in GitHub Desktop.
XCTest + async/await
import XCTest
import _Concurrency
extension XCTestCase {
func asyncTest(
expectationDescription: String? = nil,
timeout: TimeInterval = 3,
file: StaticString = #file,
line: Int = #line,
closure: @escaping () async throws -> ()
) {
let expectation = self.expectation(description: expectationDescription ?? "Async operation")
_ = Task.runDetached {
do {
try await closure()
expectation.fulfill()
} catch {
XCTFail("Error thrown while executing async function @ \(file):\(line): \(error)")
}
}
self.wait(for: [expectation], timeout: timeout)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment