Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I report an error in XCTest's setUp function?

In my UI Tests, I want to call a remote endpoint to reset db status prior to running my UI Tests. This works great, but I want to make sure I'm catching any errors if the request fails. What I want to do is:

  1. Stop the subsequent test from running (mark it as a failure)
  2. Provide some log of what happened.

Here's some sample code that illustrates my point:

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()
}
like image 934
Greg Ferreri Avatar asked Jul 09 '26 23:07

Greg Ferreri


1 Answers

setUp is part of the test run, as is tearDown. Use regular XCTest assertions in either.

like image 171
Jon Reid Avatar answered Jul 11 '26 19:07

Jon Reid