Programmatically determine current target (run or test) in iOS project Programmatically determine current target (run or test) in iOS project ios ios

Programmatically determine current target (run or test) in iOS project


None of these answers really helped me. I wanted my app to know when I was running tests for the sole purpose of limiting the logging in my test targets. The tests run faster when I'm not logging a bunch of stuff. So, I did it by adding a custom argument to the test portion of my scheme:

Scheme Screenshot

In my application code, I can now check if I am testing or not:

- (void)logError:(NSError*)error{    if([[[NSProcessInfo processInfo] arguments] containsObject:@"-FNTesting"])        return;    NSLog(@"LOG THE ERROR");}

Thanks to @cameronspickert for being one of the only places I could actually find how to use the custom argument

http://cameronspickert.com/2014/02/18/custom-launch-arguments-and-environment-variables.html


You should define proper value for "Preprocessor Macros" in Target Settings.

At the run time you can check it with ifdef sentence.


Tested on Xcode 7.3

Create a category on NSProcessInfo.

@implementation NSProcessInfo (RunningTests)- (BOOL)ag_isRunningTests {  return ([self.environment objectForKey:@"XCTestConfigurationFilePath"] != nil);}@end