»
January 21, 2009
»

Running Unittests With GDB

SenTestingKit is a nice unittesting framework for Objective-C but by default in Xcode it runs test cases as build phase so it’s not possible to attach the debuger or just see stdout output in Xcode’s Debugger Console. But it’s really easy to create Shell Tool target and executable for the project and run SenTestingKit test case class in it with or without debuger but always with stdout content available.

Here is few steps necessary to make it happen and below that the example project to try this out without messing with you existing projects.

Shell Tool Target

Create new “Shell Tool” target (I’ll name it “TestsStandalone”)

gdb_1.png

Link the newly created “TestsStandalone” target with SenTestingKit.framework (it’s located in /Developer/Library/Frameworks)

Executable’s main()

Create tests.m and add it to “TestsStandalone” target

#import <Foundation/Foundation.h>
#import <SenTestingKit/SenTestingKit.h>
#import "ZeebaTest.h"

int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   // The test case you want to run
   Class class = [ZeebaTest class];
   
   // Initialize stdout output from SenTestingKit
   [SenTestObserver class];
   
   // Run test class
   [[SenTestSuite testSuiteForTestCaseClass:class] run];
   
   [pool drain];
   return 0;
}

Add the test case you want to run (in this example ZeebaTest.m) and all dependent classes (Zeeba.m) to “TestsStandalone” target

gdb_2.png

DYLD_FALLBACK_FRAMEWORK_PATH

Set DYLD_FALLBACK_FRAMEWORK_PATH environment variable to /Developer/Library/Frameworks for “TestsStandalone” executable so SenTestingKit.framework’s dylib can be loaded at runtime.

gdb_3.png

Run

Run “TestsStandalone” target with “TestsStandalone” executable with or without debugger.

Debuger Console output:

[Session started at 2009-01-21 22:43:46 +0200.]
Test Suite 'ZeebaTest' started at 2009-01-21 22:43:46 +0200
/Users/ampatspell/Desktop/standalone/ZeebaTest.m:24: error: -[ZeebaTest testShouldFail]   ↩
  : "((@"Sorry, I'm not nil") == nil)" should be true. 
Test Case '-[ZeebaTest testShouldFail]' failed (0.004 seconds). 
2009-01-21 22:43:46.544 TestsStandalone[20392:10b] NSLog from test case what can be seen 
  when running test cases w/ TestsStandalone                                              ↩
Test Case '-[ZeebaTest testShouldSucceed]' passed (0.001 seconds).
Test Suite 'ZeebaTest' finished at 2009-01-21 22:43:46 +0200.
Executed 2 tests, with 1 failure (0 unexpected) in 0.005 (0.009) seconds


The Debugger has exited with status 0.

And Debugger with breakpoint:

gdb_4.png

That’s it.

Download “standalone.zip” (52Kb)

Using “TestStandalone” to test Frameworks

  • Make the framework you’re testing the direct dependency for “TestStandalone” target
  • Link “TestStandalone” target to your Framework

Library not loaded?

If you get something like this:

dyld: Library not loaded: @rpath/SenTestingKit.framework/Versions/A/SenTestingKit
  Referenced from: ~/Desktop/standalone/build/Debug/TestsStandalone
  Reason: image not found

then DYLD_FALLBACK_FRAMEWORK_PATH is not set correctly.

 
Internet Explorer 6
Are you serious?