Better Programming

Advice for programmers.

Follow publication

Async Setup and Teardown in XCTestCase

Matt Robertson
Better Programming
Published in
2 min readMay 26, 2022
Photo by Semyon Borisov on Unsplash

XCTestCase is kind enough to allow us to simply mark our test functions with async if we need them to be async. Unfortunately the same is not true for setUpWithError and tearDownWithError. Marking either of these async will result in the following build error:

Method does not override any method from its superclass.

Thankfully there is a fairly simple solution to this problem. Simply copy the following code into a new file and subclass XCAsyncTestCase instead of XCTestCase.

Also, be sure to override asyncSetUpWithError and asyncTearDownWithError rather than the standard setUpWithError and tearDownWithError. For an explanation of how this works, continue reading below.

The key to this solution is the wait function, which accepts an async block of code. This async block is executed inside a new task and uses a semaphore to block execution from continuing until the async block completes. The function will fire off a new async task withTask.init and signal the semaphore when it completes.

Meanwhile, execution will simply halt on the line semaphore.wait() until the semaphore has been signaled. We then use this wait function inside our typical setup and teardown functions, passing calls to our async versions instead and awaiting their completion.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Matt Robertson
Matt Robertson

Written by Matt Robertson

Android Engineer @ Crossway. Writing on clean arch, clean code, Kotlin, coroutines, & Compose.

Responses (1)

Write a response