iPhone Simulator + unittests

I just recently added the google test framework to my code and when doing so I also had may aims set at making sure I got it working on as many platforms as possible. First out was testing on OSX which worked fine and from there the next step was adding support for the iphone simulator.

Making it gtest compile for iphone/simulator wasn’t anymore trouble some than compiling other libraries cross platform. Instead, the trouble came when trying to execute the compiled tests through the simulator.

At a previous occasion, I had tried (unsuccessfully) to build and execute my entire project with the help of my own build system (which is built upon waf). I just couldn’t get the executable to start. Since then I found this link giving me the option to launch my executable explicitly:

1
    $ <path to simulator> -SimulateApplication <path to my executable>

That works, but it launches the springboard and that means that the application won’t quit like you’d expect: when you return from your main-function, the application will simply be respawned.

Instead, I looked into some other frameworks to see how they achieve this. Perhaps the most famous one is the Google Testing Framework. That package has a RunIPhoneTest.sh which contains some clues. Another clue can be found in GH-Unit that contains a RunTests.sh which is less cluttered.

In the end, it was all very simple. Set the DYLD_ROOT_PATH to the simulator sdk choice. This lets you run your executable from your command line in a straight forward manner.

Here’s a quick example.

First, save your your code into test.cpp:

1
2
3
4
5
6
#include <stdio.h>
int main(int argc, char** argv) {
    for( int i = 0; i < argc; ++i )
        printf("Arg %d: %s\\n", i, argv\[i\]);
    return 0;
}

Compile this with:

1
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++ -arch i386 -m32 -Wall -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk test.cpp -o a.out -framework OpenGLES

I added the OpenGLES framework merely to demonstrate that this test works.

Save your environment settings into a bash script runsim.sh:

1
2
3
4
5
#!/bin/sh
export DYLD\_ROOT\_PATH=/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk
#runs the executable
$@
unset DYLD\_ROOT\_PATH

Make the script executable:

1
$ chmod +x runsim.sh

Now you should be able to run your executable with:

1
$ ./runsim.sh a.out

And the expected output is:

1
$ ./a.out

Mathias Westerdahl

Mathias Westerdahl

A game developer since 2001. Currently at http://www.defold.com. Find me at twitter