Page 1 of 1

application tests crashing

PostPosted: 21 Feb 2015, 01:51
by nickhart
Hello,

I've got an iOS app with some application tests in which I use OCMock. Depending on which simulator device and OS I run them on they either all pass, or I get weird exceptions and crashes. I believe this is happening because when using an application test the test bundle is running in the test host--my real app, which is doing stuff its normal operations. Occasionally it encounters a singleton I've mocked (like +[NSUserDefaults standardUserDefaults]) and then fireworks happen when the app tries to call unexpected methods on the mock.

Does anyone have a strategy for dealing with this problem in application tests? Or do you exclusively use OCMock in logic tests? I've been trying to track down a way to detect if application tests are being run and then suspend activity in my app which might conflict with what I'm testing... but so far haven't dug up anything to help detect when these tests are running.

I've tried checking [NSBundle allBundles] in application:didFinishLaunchingWithOptions:, but it only lists the main app bundle. I've also tried looking for a TEST_HOST environment variable, but getenv() returns NULL for that.

Thanks!

Re: application tests crashing

PostPosted: 21 Feb 2015, 02:18
by nickhart
Hmm, may have found my answer! The environment variable "XCInjectBundle" seems to be present... so this code lets me know if we're running application tests or not.

static BOOL isTesting() {
NSString *injectBundle = [[[NSProcessInfo processInfo] environment] objectForKey:@"XCInjectBundle"];
return [injectBundle isKindOfClass:[NSString class]] && [injectBundle length];
}

Then in application:didFinishLaunchingWithOptions: I check isTesting() and return without kicking off the various processes that might interfere with my tests. I may need to fine tune this with time in case there is stuff I really do need to initialize and let run... but for now this seems to solve my problems.