Xcode: TEST vs DEBUG preprocessor macros

后端 未结 11 908
余生分开走
余生分开走 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

    Instead of creating a Test build configuration, I:

    1. created a Tests-Prefix.pch file:

      #define TEST 1
      #import 
      #import "CocoaPlant-Prefix.pch"
      
    2. entered its path in the Prefix Header field of the Tests target's build settings.

    3. added the following code to the top of a file I created called MyAppDefines.h, imported in MyApp-Prefix.pch:

      #ifdef TEST
      #define TEST_CLASS NSClassFromString(@"AppDelegateTests") // any test class
      #define BUNDLE [NSBundle bundleForClass:TEST_CLASS]
      #define APP_NAME @"Tests"
      #else
      #define BUNDLE [NSBundle mainBundle]
      #define APP_NAME [[BUNDLE infoDictionary] objectForKey:(NSString *)kCFBundleNameKey]
      #endif
      

    This allows me to use BUNDLE where ever I mean [NSBundle mainBundle] and also have it work when I run Tests.

    Importing SenTestingKit in Tests-Prefix.pch also speeds up the compiling of the SenTestingKit Framework and allows me to leave out #import from the top of all the tests files.

提交回复
热议问题