Created
March 3, 2021 10:36
-
-
Save kirilltitov/4600db18d64a9fc0ebc6a6939aa9ab98 to your computer and use it in GitHub Desktop.
XCTest + async/await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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