1

Using the built-in testing framework provided by Xcode, is there any way for application code to determine whether it is being run by the test runner, as opposed to running as the app?

In other words, I'm wondering whether it is possible to do something like this in the application code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ...

    if (IsUnitTestRunning()) {
        [self useDefaultSettings];
        [self showDefaultViewController];
    }
    else {
        [self restoreUserSettings];
        [self restoreUserInterface];
    }

    // ...
}

I know I could create a new configuration that defines some precompiler macros and set the Xcode scheme to build and use that configuration when running a test, or I could set some sort of global variable in my app to YES when running a test, but I'm wondering whether there is already something built into OCUnit or Xcode to handle this.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302

1 Answers1

1

On the Macintosh, there's a way to pass along some options during debug from Xcode that make it into the "int main(argc, argv)".

I suspect there's similar functionality on the iOS side. You should be able to catch options being passed in via "int main(argc, argv)".

Here's where you make the modification on the Xcode side of things:

Modify Your Xcode scheme a bit

Another option might be to look in the options dictionary passed in via "didFinishLaunchingWithOptions:" and see if there is something different about launching from Xcode versus launching it on the device or on the simulator (without being launched from Xcode).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Catching things in `main()` and/or setting environment variables does indeed work with iOS. Also see the top answer here: http://stackoverflow.com/questions/7274711/run-logic-tests-in-xcode-4-without-launching-the-simulator. (I can't find any way to set contents of the `options` dictionary, but that's not really what I want to do.) – Kristopher Johnson Apr 15 '13 at 18:46