Tuesday, April 27, 2010

iPhone App Unit Testing in Xcode

I think I have the basics of an automated unit test set up using the delivered unit testing functionality in Xcode. The instructions in the iPhone Development Guide are actually pretty good, provided you pay very close attention to the screenshots.

I followed the instructions for setting up an application test, which tests the running application as a whole as opposed to testing a particular method on a particular class. For reasons I don't understand, this type of test only can be run on the device itself, not the simulator.

Where I get myself into trouble was at step 2, "Add an iPhone OS unit-test bundle target to the project." This task is accomplished by right-clicking on Targets and selecting Add > New Target... > iPhone OS > Cocoa Touch > Unit Test Bundle.

The instructions do not mention the important next step: you have to set the identifier of the test bundle to the identifier of your app or you'll get a "code sign" error when you try to run the test. Right-click your app's icon under Targets, select Get Info > Properties, and copy the text in the Identifier field. Now right-click on the test bundle's icon under Targets, select Get Info > Properties, and paste the copied text over the contents of the Identifier field.

That should at least get you to the Writing Tests section, which is where I am now.