Skip to content

Instantly share code, notes, and snippets.

@cppforlife
Created October 6, 2011 05:13
Show Gist options
  • Save cppforlife/1266570 to your computer and use it in GitHub Desktop.
Save cppforlife/1266570 to your computer and use it in GitHub Desktop.
Run OCUnit application tests from the command line
// Run application tests from the command line:
// xcodebuild -project project.xcodeproj -target projectTests -configuration Release -sdk iphonesimulator4.3 -arch i386 TEST_HOST= BUNDLE_LOADER= TEST_AFTER_BUILD=YES build
// (How to set up: http://pivotallabs.com/users/dmitriy/blog/articles/1879-run-ocunit-application-tests-from-the-command-line)
#import <SenTestingKit/SenTestingKit.h>
#import <objc/runtime.h>
@implementation NSBundle (MainBundleHijack)
static NSBundle *mainBundle__ = nil;
NSBundle *mainBundle(id self, SEL _cmd) {
return mainBundle__;
}
+ (void)load {
if (![[[NSBundle mainBundle] bundlePath] hasSuffix:@".app"]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSBundle *bundle in [NSBundle allBundles]) {
if ([[bundle bundlePath] hasSuffix:@".octest"]) {
mainBundle__ = [bundle retain];
Class nsBundleMetaClass = objc_getMetaClass("NSBundle");
class_replaceMethod(nsBundleMetaClass, @selector(mainBundle), (IMP)mainBundle, "v@:");
}
}
[pool drain];
}
}
@end
@interface TestRunner : UIApplication
+ (void)runTests;
@end
@implementation TestRunner
+ (void)runTests {
[SenTestProbe performSelector:@selector(originalRunTests:) withObject:nil];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[self class] runTests];
return NO;
}
@end
@interface SenTestProbeHijack
@end
@implementation SenTestProbeHijack
void runTests(id self, SEL _cmd, id ignored) {
if ([UIApplication sharedApplication]) {
[TestRunner runTests];
} else {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
const char* argv[] = { "executable", "-RegisterForSystemEvents" };
int result = UIApplicationMain(2, (char **)argv, @"TestRunner", nil);
[pool release];
exit(result);
}
}
+ (void)load {
Class senTestProbeClass = objc_getClass("SenTestProbe");
if (senTestProbeClass) {
Class senTestProbeMetaClass = objc_getMetaClass("SenTestProbe");
IMP originalRunTests = class_replaceMethod(senTestProbeMetaClass, @selector(runTests:), (IMP)runTests, "v@:@");
class_addMethod(senTestProbeMetaClass, @selector(originalRunTests:), originalRunTests, "v@:@");
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment