Skip to content

Instantly share code, notes, and snippets.

@mattrob33
Created May 26, 2022 15:48
Show Gist options
  • Save mattrob33/2ab983e0f06a808becbbfadc1c326b9d to your computer and use it in GitHub Desktop.
Save mattrob33/2ab983e0f06a808becbbfadc1c326b9d to your computer and use it in GitHub Desktop.
///
/// A subclass of ``XCTestCase`` that supports async setup and teardown.
///
class XCAsyncTestCase: XCTestCase {
func asyncSetUpWithError() async throws {
fatalError("Must override")
}
func asyncTearDownWithError() async throws {
fatalError("Must override")
}
override func setUpWithError() async throws {
wait {
try await self.asyncSetUpWithError()
}
}
override func tearDownWithError() throws {
wait {
try await self.asyncTearDownWithError()
}
}
func wait(asyncBlock: @escaping (() async throws -> Void)) {
let semaphore = DispatchSemaphore(value: 0)
Task.init {
try await asyncBlock()
semaphore.signal()
}
semaphore.wait()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment