Let's Unit Test with UIKit!
Ever wanted to test UIKit classes without otest crashing? Now you can.
The test runner that's distributed with Xcode is a Mac application and isn't set up to host instances of UIKit classes. There are other options, but none of them just simply ran SenTestCase test cases at build time, so I wrote my own.
How do I use this?
- Create a new Cocoa Touch application target called 'Test Runner'.
- Edit the 'Test Runner' target and make it link against:
- SenTestingKit (this is located in /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/<SDK>/Developer/Library/Frameworks/SenTestingKit.framework)
TestRunnerMain.mto your target.
- Add a new Run Script build phase to 'Test Runner'.
- Edit that phase by double clicking on it, make sure the shell is set to /bin/sh, and the script to "PATH/TO/RunUnitTests.sh". Note that you can use the
$SRCROOTenvironment variable. See the example project for an example.
- Edit the target's Info.plist:
- Remove the 'Main nib file base name' key.
- Add the SenTestTool key with a value of YES.
- To run all tests at build time, edit your main target and add Test Runner as a direct dependecy.
For a little automation love, open the Templates folder and copy the Target Templates folder into ~/Library/Application Support/Developer/Shared/Xcode. Now you can use the Test Runner template to create a Test Runner target. You still need to add the TestRunnerMain.m file and make sure that the Run Script phase actually points to the RunUnitTests.sh script.
That's it! From now on, add any tests to the Test Runner target. Patches welcome to automate this process, perhaps with an Applescript?