XCUI setUpWithError 不等待异步函数完成执行

问题描述 投票:0回答:1

我有一个

setUpWithError
函数,我想用它来防止每个测试设置中重复代码。

在运行一些同步函数之前,我想运行几个异步函数。

到目前为止,我有这个:

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

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

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

在调用

synchronousFunction()
之前,任务中的期望似乎不会“等待”。

请问我该如何解决这个问题?非常感谢。

swift asynchronous xctest xcuitest xcuiautomation
1个回答
0
投票

如果您想在那里执行异步代码,则需要使用不同的

setUp
方法。

您需要覆盖

func setUp() async throws
。请参阅文档了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.