How to wait in a XCTest for T seconds without timeout error?

可紊 提交于 2019-12-07 05:25:49

问题


I want to delay progression of a test for T seconds, without generating a timeout.

First I tried the obvious:

sleep(5)
XCTAssert(<test if state is correct after this delay>)

But that failed.

Then I tried:

let promise = expectation(description: "Just wait 5 seconds")
waitForExpectations(timeout: 5) { (error) in
    promise.fulfill()

    XCTAssert(<test if state is correct after this delay>)
}

My XCTAssert() now succeeded. But waitForExpectations() failed with a timeout.

This is according to the documentation of XCTest wait functions saying:

Timeout is always treated as a test failure.

What are my options?


回答1:


You can use XCTWaiter.wait functions; for example:

 let exp = expectation(description: "Test after 5 seconds")
 let result = XCTWaiter.wait(for: [exp], timeout: 5.0)
 if result == XCTWaiter.Result.timedOut {
     XCTAssert(<test if state is correct after this delay>)
 } else {
     XCTFail("Delay interrupted")
 }


来源:https://stackoverflow.com/questions/50247929/how-to-wait-in-a-xctest-for-t-seconds-without-timeout-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!