Thursday, July 17, 2014

Asynchronous Unit Testing in Swift

Chris Adamson:

Fortunately, this is where Xcode 6′s asynchronous testing comes in. It allows us to create XCTestExpectation objects, which are not tests but timers. We create expectations with XCTestCase’s expectationWithDescription(), which just takes a string to describe what we’re waiting for. Then, prior to the end of the test… method, we call waitForExpectationsWithTimeout(), passing in a timeout period and a completion handler closure. This prevents the test method from exiting until either the timeout expires, or some asynchronous test code calls fulfill() on the expectation object, which unblocks it.

Update (2014-07-22): Mattt Thompson:

Perhaps the most exciting feature added in Xcode 6 is built-in support for asynchronous testing, with the XCTestExpectation class. Now, tests can wait for a specified length of time for certain conditions to be satisfied, without resorting to complicated GCD incantations.

Comments RSS · Twitter

Leave a Comment