Xcode: TEST vs DEBUG preprocessor macros

后端 未结 11 842
余生分开走
余生分开走 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 <SenTestingKit/SenTestingKit.h>
      #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 <SenTestingKit/SenTestingKit.h> from the top of all the tests files.

    0 讨论(0)
  • 2020-11-28 06:51

    Robert's answer in SWIFT 3.0:

    func isRunningTests() -> Bool {
        let environment = ProcessInfo().environment
        return (environment["XCInjectBundleInto"] != nil);
    }
    
    0 讨论(0)
  • 2020-11-28 06:52

    On iOS, [UIApplication sharedApplication] will return nil when a unit test is running.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 07:01

    If you create a Test build configuration and then set the "Other Swift Flags" property of your Target to "-DTEST" it will define a TEST macro that will work in your swift code. Make sure you set it in the build settings of your App target so that you can use it in your App's Swift code.

    Then with this set, you can test your code like so:

    func testMacro() {
       #if !TEST 
           // skipping over this block of code for unit tests
       #endif
    }
    
    0 讨论(0)
提交回复
热议问题