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)

Leave a Comment