XCTest and asynchronous testing in Xcode 6

Obj-C example: – (void)testAsyncMethod { //Expectation XCTestExpectation *expectation = [self expectationWithDescription:@”Testing Async Method Works!”]; [MyClass asyncMethodWithCompletionBlock:^(NSError *error, NSHTTPURLResponse *httpResponse, NSData *data) { if(error) { NSLog(@”error is: %@”, error); }else{ NSInteger statusCode = [httpResponse statusCode]; XCTAssertEqual(statusCode, 200); [expectation fulfill]; } }]; [self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) { if(error) { XCTFail(@”Expectation Failed with error: %@”, error); } }]; … Read more

Xcode 7 UI Testing: how to dismiss a series of system alerts in code

Xcode 7.1 Xcode 7.1 has finally fixed the issue with system alerts. There are, however, two small gotchas. First, you need to set up a “UI Interuption Handler” before presenting the alert. This is our way of telling the framework how to handle an alert when it appears. Second, after presenting the alert you must … Read more

Is there a way to reset the app between tests in Swift XCTest UI?

You can add a “Run Script” phase to build phases in your test target to uninstall the app before running unit tests against it, unfortunately this is not between test cases, though. /usr/bin/xcrun simctl uninstall booted com.mycompany.bundleId Update Between tests, you can delete the app via the Springboard in the tearDown phase. Although, this does … Read more