如何在XCTest中等待T秒而没有超时错误? [英] How to wait in a XCTest for T seconds without timeout error?

查看:309
本文介绍了如何在XCTest中等待T秒而没有超时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将测试进度延迟T秒,而不会产生超时.

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>)

但是失败了.

然后我尝试了:

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

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

我的XCTAssert()现在成功.但是waitForExpectations()失败并超时.

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

这是根据 XCTest等待函数的文档进行的说:

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

超时总是被视为测试失败.

Timeout is always treated as a test failure.

我有什么选择?

推荐答案

您可以使用 XCTWaiter.wait 函数;例如:

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")
 }

这篇关于如何在XCTest中等待T秒而没有超时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆