XCUI setUpWithError not waiting for async functions to finish executing

86 Views Asked by At

I have a setUpWithError function that I want to use to prevent duplicating code in each of the test's setup.

I have a couple of asynchronous functions that I want to run, prior to running some synchronous functions.

So far, I have this:

override func setUpWithError() throws {
    let expectation = XCTestExpectation(description: "Setup")

    Task {
        await function1()
        await function2()
        expectation.fulfill()
    }

    wait(for: [expectation], timeout: defaultTimeout)
    
    synchronousFunction()
}

The expectation within the task doesn't appear to "wait" before the synchronousFunction() is called.

How can I resolve this please? Many thanks.

1

There are 1 best solutions below

1
On

You need to use a different setUp method if you want to execute async code there.

You need to override func setUp() async throws. See the docs for more information.