swift - 如何报告XCTest的setUp函数中的错误?

标签 swift xctest xcode-ui-testing

在我的 UI 测试中,我想在运行 UI 测试之前调用远程端点来重置数据库状态。这很好用,但我想确保在请求失败时捕获任何错误。我想做的是:

  1. 停止运行后续测试(将其标记为失败)
  2. 提供一些发生情况的日志。

这里有一些示例代码来说明我的观点:

override func setUp() {
    super.setUp()

    var finished = false

    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
        let request = Alamofire.request(.GET, "http://xxx.xxx.xxx.xxx/resetdb")
        request.response() {
            request, response, data, error in
            if let _ = error {
                let message = "Could not call remote helper -- \(response?.statusCode)"
                XCTFail(message)  // doesn't work
                debugPrint(message) // can't see this anywhere
            }
            finished = true
        }
    }

    while !finished {
        NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture())
    }
    app = XCUIApplication()
    app.launch()
}

最佳答案

setUp 是测试运行的一部分,tearDown 也是如此。在任一中使用常规 XCTest 断言。

关于swift - 如何报告XCTest的setUp函数中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35583050/

相关文章:

iOS UITests - 如何区分两个不同的 XCUIElement?

swift - Xcode 8 : UI Testing, 任何后代元素都没有键盘焦点

ios - UITableViewCell 内容被剪切

json - Alamofire 4、 swift 3 : Can not return StatusCode

ios - 如何将子类 View Controller 的 View 放入父类(super class)基 View Controller 的内容 View 中?

ios - 使用 tableview 设置 uisearchdisplaycontroller 时出现问题 - Swift

javascript - 尝试在 XCTest 中进行模板测试

objective-c - XCTest:[UIApplication sharedApplication] 返回 nil

swift - XCTAssertNotNil 在 Swift 2 中什么都不做

ios - Xcode UI 测试 - 找不到刚刚记录的元素