views:

36

answers:

2

I have the following minimal test case in a minimal project created following the GHUnit README:

#import <GHUnitIOS/GHUnitIOS.h>
#import <UIKit/UIKit.h>

@interface MyTest : GHTestCase { }
@end

@implementation MyTest

- (BOOL)shouldRunOnMainThread {
  return YES;
}

- (void)testFoo {
  UITableViewCell *cell =
      [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain
                             reuseIdentifier:@"foo"];

  NSLog(@"cell: %@", cell);
  NSLog(@"cell.textLabel: %@", cell.textLabel);
}

@end

It runs fine when I use Build and Run in the simulator in Xcode. However, when I run the following command in the terminal:

GHUNIT_CLI=1 xcodebuild -target Tests -configuration Debug -sdk iphonesimulator4.0 build

I get this output:

Running: /Users/<user>/Desktop/tmp/TestApp/build/Debug-iphonesimulator/Tests.app/Tests -RegisterForSystemEvents
Tests(39346) malloc: protecting edges
Tests(39346) malloc: recording malloc stacks to disk using standard recorder
Tests(39346) malloc: enabling scribbling to detect mods to free blocks
Tests(39346) malloc: process 39249 no longer exists, stack logs deleted from /tmp/stack-logs.39249.Tests.ac1JfL.index
Tests(39346) malloc: stack logs being written into /tmp/stack-logs.39346.Tests.t8LG4p.index
Test Suite 'Tests' started.
MyTest/testFoo 2010-09-06 23:24:25.006 Tests[39346:903] cell: <UITableViewCell: 0x5a6d190; frame = (0 0; 320 44); layer = <CALayer: 0x5a6d390>>
RunTests.sh: line 28: 39346 Trace/BPT trap          $RUN_CMD
Command /bin/sh failed with exit code 133
Command /bin/sh failed with exit code 133
** BUILD FAILED **

This "Trace/BPT Trap" thing happens with OCUnit too, and I was hoping GHUnit would solve it, but it doesn't on the command line. Anybody have any idea what it's about? It seems to have to do with using UIKit from a context you're not supposed to, but I don't understand what exactly the restriction is.

A: 

Ah, I added some more code and figured out that [NSApplication sharedApplication] is null when I run on the command-line; I assume it has to do with that. So is there an easy way to run unit tests that use UIKit classes without running the simulator?

Aaron Jacobs
A: 

I've had similar crashes when testing UIViews and UIViewControllers in GHUnit, when running from the command line because there is no interface for the views to be drawn to.

You should be able to test the logic in some of these classes as long as you don't call methods that cause them to draw themselves to the screen. You can init a view as long as it's not added to the main view hierarchy. Steering clear of drawRect in UIView and loadView in UIViewController was a help for me.

However, if you've got the kind of logic in your views that needs testing, maybe it would be easier to move the logic?

Stew