Friday, October 17, 2008

C++ unit testing for Iphone with googletest

Following the instructions on the googletest googlecode project faq will lead you inevitably to this error message:

"target specifies product type '', but there's no such product type for the 'iphonesimulator' platform"

The reason this happens is that you need to create a command line utility in xcode to run your tests but the iphone sdk doesn't have that project type.

The good news is there is a workaround, the bad news is you will need to create a separate project for your tests to get around this. You will also have to separate the iphone cocoa objective C stuff from the real brains of your app by delegating to some good old C++. This turns out to be easy because objective c is a superset of C++ so you can forget all that cocoa stuff Apple is trying to get you to learn and when the time comes to do something interesting in your iphone opengl ES app, you can just call your C++ classes directly. This also has the side benefit of making your code testable using standard tools.

You'll have to try and remove any kind of dependencies from Apple libraries from your C++ code. A separation of concerns.

Follow the standard GoogleTest installation instructions for your new BSD project:

1. Download the source from the website using this command: svn checkout googletest-read-only
2. Open up the gtest.xcodeproj in the googletest-read-only/xcode/ directory and build the gtest.framework.
3. Create a new "Shell Tool" target in your BSD Xcode project called something like "UnitTests"
4. Add the gtest.framework to your project and add it to the "Link Binary with Libraries" build phase of "UnitTests"
5. Add your unit test source code to the "Compile Sources" build phase of "UnitTests"
6. Edit the "UnitTests" exectable and add an environment variable named "DYLD_FRAMEWORK_PATH" with a value equal to the path to the framework containing the gtest.framework relative to the compiled executable.
7. Build and Go

Some additional notes for this to work with opengl es.

Create a separate Xcode BSD project.
create a new folder for references to the cocoa opengl project files.
drag over your C++ code from the iphone opengl project to the BSD project. Don't Copy the files! Just make references so you can edit once in both projects.
Drag the cpp files you are testing into the "compile sources" directory as you need them.

Should look something like this:

command + shift + R to see test results.

No comments:

Post a Comment