Xcode: TEST vs DEBUG preprocessor macros

Preprocessor macros will not work, you need to check the environment at runtime.

Objective-c

static BOOL isRunningTests(void)
{
    NSDictionary* environment = [[NSProcessInfo processInfo] environment];
    return (environment[@"XCTestConfigurationFilePath"] != nil);
}

Swift

var unitTesting : Bool 
{
    return ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil
}

(Updated for Xcode 11)


You might consider adding a new build configuration.

In xcode 4, click on your project on the left hand navigator.

In the main window, click on your project, and then select the "info" tab.

Click the "+" button to add a new configuration (you can call yours "test" if you like").

Now, click on your target, and go to the build settings tab.

Search for "preprocessor macros"

Here, you can add preprocessor macros for your new build configuration.

Just double click on your new "test" configuration, and add TESTING=1.

Finally, edit your build scheme. Select the test options for your scheme. There should be a "Build Configuration" drop down menu. Select your "test" configuration.