Using Xcode to Unit Test SDL Games Written in C++
Suppose you’re using SDL to program a game. You’re writing the game in C++, and you want to write unit tests for your code. Your first instinct would be to use a C++ unit testing framework like googletest or UnitTest++. Your instinct would be correct on Linux and Windows, but the Mac version of SDL uses Cocoa, which is an Objective-C framework. If you try to unit test your SDL C++ game on a Mac, you’re going to get hundreds of compiler errors related to the Cocoa framework because a C++ unit testing framework doesn’t understand Objective-C. How do you unit test your SDL game on a Mac?
Use Xcode’s built-in support for Objective-C unit testing. Add a Cocoa unit testing bundle target to your project. Add an Objective-C test case class to your project, making sure to add the test case class to the unit testing target, not the target for your SDL game. Give the test case file the extension .mm instead of .m. Giving the file the extension .mm tells Xcode to treat the test case file as an Objective-C++ file, which allows you to unit test your C++ code with OCUnit, the Objective-C unit testing framework that comes with Xcode.
Make sure your game’s C++ source files are members of the unit testing target you created. If you don’t add your game’s C++ files to the unit testing target, you will get linker errors when your unit tests access your game’s classes. Read my googleTest Xcode Tip post for instructions on adding your C++ files to the unit testing target.
Apple’s Xcode Unit Testing Guide, which is part of Apple’s Mac developer documentation, contains additional information on setting up Xcode for unit testing. Also, thanks to Chris Hanson for his answer to a question 5 years ago on one of Apple’s mailing lists. Without his answer I would have never known you could unit test C++ code with OCUnit.
googletest Xcode Tip
Make sure your application’s C++ source files are members of the unit testing target you created. If you don’t add your application’s C++ files to the unit testing target, you will get linker errors when your unit tests access your application’s classes.
To add one of your application’s C++ files to the unit testing target, open the file’s inspector by selecting the file in the project window and clicking the Get Info button in the project window toolbar. Click the Targets tab in the inspector, which should have a list of your project’s targets with a checkbox next to each target. Select the checkbox next to the unit testing target.

Alternatively, you can add a bunch of files to the unit testing target by clicking disclosure triangles in the Groups and Files list to expose the unit testing target’s build phases. Drag the C++ files to the unit testing target’s Compile Sources build phase.
Mac Game Programming Roadmap Update for 2010
I’ve seen several questions on Mac programming forums recently looking for Mac game programming tutorials, which made me look at my Mac Game Programming Roadmap article. The article is three years old, and some things have changed in the past three years.
Avoid Carbon
Unless you have an existing Carbon codebase, avoid Carbon. If you don’t want to learn Objective-C, use a cross-platform game framework like SDL, Allegro, or SFML. Another advantage of using one of these frameworks is you can support Windows and Linux without having to rewrite much of your code.
SFML
SFML is a cross-platform game framework that has grown in popularity in the past three years. I have not used SFML, but go the SFML website and see if it meets your needs.
Unity
Unity existed in 2007, but its Indie version cost $199. The Indie version is now free and lets you write games that run on Mac OS X, Windows, and the Web. If you’re interested in creating 3D games, I recommend Unity. You’ll finish your game much faster with Unity than you will by programming your game from scratch.
Loading Files for Unit Testing in Cocoa
Adding files to your unit testing bundle helps when you’re unit testing file behavior or when you have a lot of test data. This post shows you how to load files from the unit testing bundle to help you unit test Cocoa applications.
Before You Code
The first thing you must do is get your test files in the unit testing bundle. Add the test files to your project. When adding the files, add them to the unit testing target, not the application target. Make sure the test files are part of the unit testing target’s Copy Bundle Resources build phase. When you build the project, the test files will be copied to the unit testing bundle’s Resources directory.
Loading a File from the Unit Testing Bundle
To load an individual file call NSBundle’s pathForResource: method. The following code loads a test XML file from the unit testing bundle:
NSBundle *unitTestBundle = [NSBundle bundleForClass:[self class]];
NSString* xmlFilename = [unitTestBundle pathForResource:@"XMLTestFile" ofType:nil];
NSData* xmlData = [[NSData alloc] initWithContentsOfFile:xmlFilename];
Opening a File Wrapper
To open a file wrapper, call NSBundle’s URLForResource: method to find the file wrapper. Call NSFileWrapper’s initWithURL: method to open the file wrapper. The following code opens a file wrapper from the unit testing bundle:
NSBundle *unitTestBundle = [NSBundle bundleForClass:[self class]];
NSURL* testFileURL = [unitTestBundle URLForResource:@"FileWrapperTest" withExtension:@"wrap"];
NSFileWrapper* testFile = [[NSFileWrapper alloc] initWithURL:testFileURL options:0 error:nil];
Substitute your file wrapper’s file extension for “wrap” in the URLForResource: call. Read my Working with Cocoa File Packages post for more information on working with file wrappers.
Keep in mind that the initWithURL: method was added in Mac OS X 10.6. If you’re running an older version of Mac OS X, call NSFileWrapper’s initWithPath: method, which was deprecated in 10.6.