One annoying thing when running tests in Xcode 6.1 is that the entire app has to run and launch its storyboard and root view controller. In my app this runs some server calls th
I believe it's completely legitimate to want to know if you're running inside a test or not. There are numerous reasons why that can be helpful. For example, in running tests, I return early from application-did/will-finish-launching methods in the App Delegate, making the tests start faster for code not germane to my unit test. Yet, I can't go pure "logic" test, for a host of other reasons.
I used to use the excellent technique described by @Michael McGuire above. However, I noticed that stopped working for me around Xcode 6.4/iOS8.4.1 (perhaps it broke sooner).
Namely, I don't see the XCInjectBundle anymore when running a test inside a test target for a framework of mine. That is, I'm running inside a test target that tests a framework.
So, utilizing the approach @Fogmeister suggests, each of my test schemes now sets an environment variable that I can check for.
Then, here's some code I have on a class called APPSTargetConfiguration
that can answer this simple question for me.
static NSNumber *__isRunningTests;
+ (BOOL)isRunningTests;
{
if (!__isRunningTests) {
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *isRunningTestsValue = environment[@"APPS_IS_RUNNING_TEST"];
__isRunningTests = @([isRunningTestsValue isEqualToString:@"YES"]);
}
return [__isRunningTests boolValue];
}
The one caveat with this approach is that if you run a test from your main app scheme, as XCTest will let you do, (that is, not selecting one of your test schemes), you won't get this environment variable set.