Unit testing on the iPhone and the Simulator

by Julian on June 29th, 2009

A recent post over on Acorn Heroes, “Unit Testing and linking Static Libraries with XCode”, has got me thinking: I went through a similar process a few months ago, when I was integrating UnitTest++ into my build environment. I use external makefiles to build static C++ libraries and the associated tests, and although the standard approach works well for the simulator, I still haven’t found a decent solution for the running on the iPhone itself (read more below).

Unit testing on the iPhone Simulator

Although this is a solved issue (using the “google-toolbox-for-mac” approach), it still runs UI-less, which most of the time is fine. Approaches such as using OCMock look great, but at the time I wanted to see if I could get unit testing running on the simulator with the UI.

Using a combination of tools like fseventer (which observes filesystem changes) and Peek-a-Boo (which observes processes), I set about documenting the application startup process (on the simulator). From this, I cobbled together a script which I’d hope would do the trick… but unfortunately not :( . But I think it must be close!

I’ve put the salient points of the script at the end of the post, in case someone else has a solution.

Unit testing on the iPhone

Running tests from the command line (built via makefile) on the device is what I’d really like to find an answer to, but I haven’t really got very far with this :(

The strongest candidate at the moment may well be a combination of CMake to generate an XCode project file, and then using Applescript to launch the app, as others are discussing here. Unfortunately the command-line tool xcodebuild doesn’t appear to have an option to launch applications.

Do you know of any alternatives?

For running test sub-projects (not built from the command line) the article Unit Testing Applications on the Apple developer site is definitely worth reading, especially the section “Setting up Application Testing”, but again doesn’t help in my situation.


“Script” for running unit tests on the iPhone Simulator

Finally, here are the snippets of the script I talked about earlier. I haven’t had time to revisit this since the release of OS 3.0, but if anyone else has tried anything similar I’d be very interested in hearing about it.  As I mentioned above, this script doesn’t work, so is only here for reference!

# Start the simulator, but wait...
"/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app/Contents/MacOS/iPhone Simulator" -SessionOnLaunch NO &

# Get the pid of the simulator, we'll need that in a moment
pid=`ps wwax | egrep -i '[i]Phone Simulator' | awk '{ print $1 }'`

Now I found I had to set the usual environment variables, as per the standard approach, namely, DYLD_ROOT_PATH, DYLD_FRAMEWORK_PATH, IPHONE_SIMULATOR_ROOT and CFFIXED_USER_HOME.

# Start the simulator bridge
"/Developer/Platforms/iPhoneSimulator./Developer/Applications/iPhone Simulator.app/Contents/MacOS/SimulatorBridge" $pid &

# Had to unset some environment variables for the following commands to work.
unset DYLD_FRAMEWORK_PATH

The following is where it gets a bit odd. I noticed a sequence of launchd, installd and securityd (which may need to be run as another user, possibly _securityd).

#launchd - not sure what this is doing perhaps handling the following sequence.

# Run installd with a timeout of 30 seconds
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr/libexec/installd -t 30 &

# Run securityd - think this is something to do with entitlements
#/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr/libexec/securityd

# Now start SpringBoard
"/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/System/Library/CoreServices/SpringBoard.app/SpringBoard" \
    -SBDisableAutoDim YES \
    -SBAutoLockTime -1 \
    -SBAutoDimTime -1 \
    -SBDontLockAfterCrash YES \
    -SBDidShowReorderText YES \
    -SBFakeBars YES \
    -SBDontAnimateAppleDown YES \
    -SBEnableDoubleHeightToggling YES &

# Now start the app, which you'll find in some auto-generated folder in
"/Users/.../Library/Application Support/iPhone Simulator/User/Applications/.../test.app/test"

There’s not a huge point in chasing a solution to this broken script, as the UI-less testing works so well. But, have you tried anything like this at all? It strikes me that the jail-breaking crowd may know more.

From iphone

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS