A couple weeks ago as part of my Unit Testing Series I talked about how to use Xcode to write unit tests for Cocoa frameworks, debug unit tests in Cocoa frameworks, and write unit tests for Cocoa applications. However, I haven’t yet described how to debug your unit tests in Objective-C Cocoa applications. I’ll take care of that tonight.
After you’ve set up unit testing in your Cocoa application, debugging your unit tests is similar to debugging them in a Cocoa framework. All you have to do is adjust the arguments and environment variables your application’s Executable is configured to use in Xcode. You don’t even have to create a new executable.
To start, bring up the Info window for your application’s executable (which is its entry in Xcode’s Executable smart group). In the Arguments tab, add the argument -SenTest All
. This tells the unit testing infrastructure that you want to run all of the unit tests, not just the ones that are built in to the executable. (After all, you don’t have any unit tests in your executable itself.)
Now we’ll need to engage in a little bit of environment variable magic. When you test an application, what you’re really doing is injecting your unit test bundle into the application and telling it to run its tests at its first opportunity. This is accomplished through by telling dyld
to insert a private framework, DevToolsBundleInjection.framework
, into your application on launch, and telling that framework via an environment variable, XCInjectBundle
, the path of the test bundle to inject.
You also have to tell the injection framework itself the full path to the application executable you want to inject the bundle into, via the XCInjectBundleInto
environment variable. This is needed to avoid injecting your test bundle into other executables that are run by the application you’re testing, or that are run as a side-effect of running the application you’re testing. (For example, gdb
generally wants to run applications from within a shell, so that environment variables are expanded in its environment and command-line parameters.)
In the Arguments tab of your application executable, first add an environment variable named DYLD_INSERT_LIBRARIES
. Set its value to the path to the DevToolsBundleInjection.framework/DevToolsBundleInjection
library in the framework of the same name that’s included in the developer tools. Prior to Xcode 2.5, this was in $(SYSTEM_LIBRARY_DIR)/PrivateFrameworks
but as of Xcode 2.5 and Xcode 3.0, it has been moved to $(DEVELOPER_LIBRARY_DIR)/PrivateFrameworks
Then add a second environment variable, XCInjectBundle
. Set its value to $(BUILT_PRODUCTS_DIR)/MyTestBundle.octest
.
Add a third environment variable, XCInjectBundleInto
. Set its value to the full path to your application’s executable — not just the application bundle — e.g. $(BUILT_PRODUCTS_DIR)/MyApplication.app/Contents/MacOS/MyApplication
. This is the debugging equivalent of the Test Host build setting you used to tell Xcode what executable to inject your tests into when running them.
For Xcode 3.0 and later, add a final environment variable, DYLD_FALLBACK_FRAMEWORK_PATH
to your executable. Set its value to $(DEVELOPER_LIBRARY_DIR)/Frameworks
.
Why do you need to do this? In order to support moving and renaming the Developer folder, all of the frameworks within it — including OCUnit — use runpath search paths. This means that the internal name of the framework, including the one copied into your test bundle, will start with @rpath
rather than an absolute path starting with /Developer/Library/Frameworks
. Unfortunately this means that your unit tests won’t find SenTestingKit.framework
without some extra help. That’s what DYLD_FALLBACK_FRAMEWORK_PATH
does: It tells dyld
to try an additional set of directories in place of @rpath
when it can’t be resolved. (More information on runpath-relative install names can be found in the ld(1)
man page.)
Make sure the check marks next to all three of these environment variables — and your -SenTest All
argument, of course — are set.
Troubleshooting note: Troubleshooting note: If this doesn’t work — that is, if your test bundle isn’t found and run — change the executable’s working directory (in the General tab) to Built Products Directory and remove $(BUILT_PRODUCTS_DIR)
above. Generally this is caused by $(BUILT_PRODUCTS_DIR)
not being expanded to a full path, but rather to a partial path relative to your project directory.
Now if you choose Run Executable from the Debug menu, your application should launch, you should see the results of executing your unit tests in the Run Log, and as soon as your unit tests are complete your application should quit!
To debug a failing test, build your tests and set a breakpoint on the line where the failure occurs. Now choose Debug Executable from the Debug menu. As with a Cocoa framework, do not choose Build and Debug from the Build menu. You need to use Debug Executable because your build will fail due to the failing test. Debug Executable will work as long as your executable itself is actually present.
Having done all this, you should be stopped at the breakpoint!
Just as any other time you use OCUnit, instead of -SenTest All
you can specify -SenTest MyTestCaseClassName
to run just the tests in the test case class MyTestCaseClassName
, or -SenTest MyTestCaseClassName/testMethodName
to run just a single test.
Update July 7, 2007: Added the troubleshooting note about removing $(BUILT_PRODUCTS_DIR)
if you get errors about not being able to load the bundle.
Update March 17, 2008: I’ve updated this a bit to handle some changes in the process introduced with Xcode 3.0.
Update September 5, 2008: I’ve updated this again to cover the changes that were made to bundle injection for Xcode 3.1; the change is the introduction of the XCInjectBundleInto
environment variable.