Daniel Tull: Blog

Retrofitting Asynchronous Unit Testing in Xcode 5

Thursday, 10 July 2014

With Xcode 6, Apple have finally given us a sanctioned way to perform asynchronous unit tests. For more information about this you can watch the WWDC 2014 session Testing in Xcode 6 or read the new Testing with Xcode document.

Both of these show you how to adopt the new methods in Xcode 6, however some of us still need to build with Xcode 5.1 in the hope that we’ll ship before Xcode 6 is released, but may still want to test asynchronous methods now.

Previously, I’ve used techniques similar to Mike Ash’s NSRunLoop-based example to cause the test to wait for my async code to finish running, which has worked well. Using this approach, I have recreated the basics of Apple’s asynchronous XCTest methods in a project called DCTAsynchronousTesting.

The following shows how you would use this code, it might look extremely similar to examples from Apple’s documentation.

@import XCTest;
#import <AsynchronousTesting/AsynchronousTesting.h>
#import "XCTestCase+DCTAsynchronousTesting.h"

@interface AsynchronousTestingTests : XCTestCase
@end

@implementation AsynchronousTestingTests

- (void)testExample {

	XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
	[AsynchronousTesting performAsynchronousOperationWithCompletion:^(BOOL success) {
		XCTAssertTrue(success, @"Operation should have succeeded.");
		[expectation fulfill];
	}];

	[self waitForExpectationsWithTimeout:2 handler:nil];
}

@end

Hopefully this will help you write Xcode 6 compatible asynchronous tests until the “fall” comes.