我是 XCTest UI 测试的新手。我创建了一个用于 Xcode 持续集成的机器人。我添加了 iPhone 4s、5s 和 6 进行并行测试。
测试很简单。当我单击按钮时,带有“Hai”的标签应更改为“Hello”。我刚刚断言最终标签值为“Hello”。当我在 Xcode 中本地运行测试时,它适用于所有设备。但当使用机器人进行提交和集成时,测试用例仅针对 iPhone 4s 失败,表明
Bot Issue for Begin Bot (test failure in -[BeginUITests testExample()]) Integration #41 of Begin Bot
Assertion: XCTAssertEqual failed: ("Optional("Hai")") is not equal to ("Optional("Hello!")") - File: Begin/BeginUITests/BeginUITests.swift:43
不知道测试失败的原因。我编写的测试用例如下所示:
func testExample() {
let app = XCUIApplication()
let firstLabel = app.staticTexts.elementBoundByIndex(0)
let button = app.buttons["Button"]
XCTAssert(button.exists)
XCTAssert(firstLabel.exists)
button.tap()
sleep(3)
let changedLabel = app.staticTexts.elementBoundByIndex(0)
XCTAssertEqual(changedLabel.label, "Hello!")
}
最佳答案
4S 在您的 CI 服务器上可能比在本地计算机上稍慢,并且通常比后续设备的模拟器运行得更慢。
如果在点击完成后对 View 层次结构进行采样之前操作未完成,则放置 sleep(3)
不会有帮助,因为 View 层次结构不一定会在点击完成后的任何时候刷新。水龙头。
为了解决这个问题,请使用期望,这将确保在每次检查之前刷新所针对的断言的 View 层次结构。
expectationForPredicate(NSPredicate(format: "label == 'Hello!'"), evaluatedWithObject: changedLabel, handler: nil)
waitForExpectationsWithTimeout(5, handler: nil)
关于swift - iPhone 4s 设备中的 XCTest 持续集成失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39095401/