Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created January 15, 2014 14:03
Show Gist options
  • Save rsaunders100/8436798 to your computer and use it in GitHub Desktop.
Save rsaunders100/8436798 to your computer and use it in GitHub Desktop.
To prevent application logic from interfering with unit tests.
static BOOL isRunningTests(void) __attribute__((const));
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (isRunningTests()) {
return YES;
}
//
// Normal logic goes here
//
return YES;
}
static BOOL isRunningTests(void)
{
NSDictionary* environment = [[NSProcessInfo processInfo] environment];
NSString* injectBundle = environment[@"XCInjectBundle"];
NSString* pathExtension = [injectBundle pathExtension];
return ([pathExtension isEqualToString:@"octest"] ||
[pathExtension isEqualToString:@"xctest"]);
}
@rsaunders100
Copy link
Author

This is a modified version of the snippet from this excellent article:
http://www.objc.io/issue-1/testing-view-controllers.html

I added compatibility for XCode5's newer XCTest framework.

This is also mentioned in my blog post:
http://www.rsaunders.co.uk/2014/01/preventing-unit-tests-from-crashing.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment