Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

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

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"]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...