Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

testing

Is there any way to programmatically determine if you are running code in the Test target vs the regular Run target while developing iOS applications?

I have the hack of checking if this variable is nil or not as it is only in my test target but this seems pretty hacky.

[[[NSProcessInfo processInfo] environment] objectForKey:@"XCInjectBundle"]
like image 582
fzf Avatar asked Jan 11 '13 04:01

fzf


1 Answers

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

like image 80
ganta Avatar answered Sep 22 '22 16:09

ganta