Xcode: TEST vs DEBUG preprocessor macros

后端 未结 11 922
余生分开走
余生分开走 2020-11-28 06:24

When creating a new project with unit tests, Xcode sets the build configuration to Debug for the Test scheme (same for the Run scheme).

Should I differentiate betwee

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 06:50

    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)

提交回复
热议问题