Jump to content

How to do UI Testing of iOS Applications Using OCUnit

+ 2
  blackbear's Photo
Posted Aug 01 2011 07:44 PM

Until recently, the only want to do UI testing of your iOS applications was to use the UIAutomationFramework, which was buggy, very difficult to code for, and could not be run in an automated fashion, because it required you to run it under the Instruments tool.

Apple very quietly snuck a feature into recent versions of XCode. You may have noticed that when you run testing in XCode using the Test (⌘-U) command, the simulator starts up now, and the tests run on the simulator. This is as opposed to previously, which the tests ran during compilation (which, among other things, meant that you couldn't breakpoint inside OCUnit tests.)

What may not be obvious is that the tests run with the entire application up and running, which means that you can manipulate and test functionality by setting field values, explicitly calling event handlers, etc. How do you do it? In your unit test, add this code:


- (void)setUp
{
    [super setUp];
    delegate = [[UIApplication sharedApplication] delegate];
    
}




The delegate variable should be a class property that is of the type of the application delegate your app uses. Then, in a test, you can do things such as this:


-(void) testZipCode {
    RootViewController *controller = delegate.rootViewController;
    DetailViewController *detail = controller.detailViewController;
    detail.outputView.text = @"";
    [detail lookupZipCode:nil];
    [[NSRunLoop currentRunLoop] runUntilDate:
                   			[NSDate dateWithTimeIntervalSinceNow: 20]];
    STAssertTrue([detail.outputView.text length] > 0, @"Detail view is blank");
}


You can get access to the view controllers, set field values, call an event handler, wait a bit for an asynchronous network reply, and then check that the value was set correctly.

This new functionality is going to open up a whole new world of testability for iOS applications, especially since it should be possible to call them from the command line, letting them be incorporated into continuing integration build processes. Look for more complete examples in my new O'Reilly book, Developing Enterprise iOS Applications, due out right after the iOS 5 release.

Developing Enterprise iOS Applications

Learn more about this topic from Developing Enterprise iOS Applications.

If you plan to develop iOS applications in a corporate setting—for internal consumption or for sale to end users—you need to read this book. Veteran developer James Turner shares best practices and lessons learned from his recent on-the-ground experience planning, building, and shipping an iOS application in an enterprise environment.

See what you'll learn





Tags:
1 Subscribe


0 Replies