Xcode: TEST vs DEBUG preprocessor macros

后端 未结 11 911
余生分开走
余生分开走 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 07:00

    I decided to add a check for an environment variable in the code itself, instead of using the isRunningTests() suggestion Robert made.

    1. Edit the current Scheme (Product/Scheme/Edit Scheme) or Command+<
    2. Click on the Test configuration
    3. Click on Arguments Uncheck "Use the Run action's arguments and environment variables
    4. Expand Environment Variable section and add the variable TESTING with value YES
    5. Add this to your code somewhere and call is whenever you need to:
     + (BOOL) isTesting
        {
            NSDictionary* environment = [[NSProcessInfo processInfo] environment];
            return [environment objectForKey:@"TESTING"] != nil;
        }
    

    The screen should look like this when you are done.

    The screen should look like this

    The code above will find the TESTING environment variable when running in test mode or application mode. This code goes in your application, not the unit test files. You can use

    #ifdef DEBUG
    ...
    #endif
    

    To prevent the code from being executed in production.

提交回复
热议问题