Thanks @steven-hepting, your answer helped me pointing in the correct direction to solve my issue.
But when using "Host Application" in your unit tests "XPC_SERVICE_NAME" will return the same string as normal app start (obviously). So your check alone doesn't always work. That is why I'm also checking TestBundleLocation
in addition. Tested this with Xcode 7.2 (7C68).
+ (BOOL)isRunningUnitTests {
NSDictionary<NSString *, NSString *> *env = [NSProcessInfo processInfo].environment;
// Library tests
NSString *envValue = env[@"XPC_SERVICE_NAME"];
BOOL isTesting = (envValue && [envValue rangeOfString:@"xctest"].location != NSNotFound);
if (isTesting) {
return YES;
}
// App tests
// XPC_SERVICE_NAME will return the same string as normal app start when unit test is executed using "Host Application"
// --> check for "TestBundleLocation" instead
envValue = env[@"TestBundleLocation"];
isTesting = (envValue && [envValue rangeOfString:@"xctest"].location != NSNotFound);
return isTesting;
}