Xcode: TEST vs DEBUG preprocessor macros

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

    You might consider adding a new build configuration.

    In xcode 4, click on your project on the left hand navigator.

    In the main window, click on your project, and then select the "info" tab.

    Click the "+" button to add a new configuration (you can call yours "test" if you like").

    Now, click on your target, and go to the build settings tab.

    Search for "preprocessor macros"

    Here, you can add preprocessor macros for your new build configuration.

    Just double click on your new "test" configuration, and add TESTING=1.

    Finally, edit your build scheme. Select the test options for your scheme. There should be a "Build Configuration" drop down menu. Select your "test" configuration.

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

    Look at environment variables to see if unit tests are running. Similar to Robert's answer but I only check once for performance sake.

    + (BOOL)isRunningTests {
       static BOOL runningTests;
       static dispatch_once_t onceToken;
    
       // Only check once
       dispatch_once(&onceToken, ^{
          NSDictionary* environment = [[NSProcessInfo processInfo] environment];
          NSString* injectBundle = environment[@"XCInjectBundle"];
          NSString* pathExtension = [injectBundle pathExtension];
          runningTests = ([pathExtension isEqualToString:@"octest"] ||
                          [pathExtension isEqualToString:@"xctest"]);
       });
       return runningTests;
    }
    
    0 讨论(0)
  • 2020-11-28 06:42

    Updated for Xcode10:

    static let isRunningUnitTests: Bool = {
        let environment = ProcessInfo().environment
        return (environment["XCTestConfigurationFilePath"] != nil)
    }()
    
    0 讨论(0)
  • 2020-11-28 06:46

    I test such a long time, found a result:

    Not only you add the preproessor macro into your unit test target(You could have many methods using variables for unit testing only, and follow the @MattDiPasquale methods),

    but also You must add the condition complie file in your test target. we should recomplie this file, because you have a new preprocessor macro for this file, but this file has built in the application target that time your preprocessor macro didn't set.

    Hope this help you.

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

    Modified version of Kev's answer that works for me on Xcode 8.3.2

    +(BOOL)isUnitTest {
    
        static BOOL runningTests;
        static dispatch_once_t onceToken;
    
        // Only check once
        dispatch_once(&onceToken, ^{
            NSDictionary* environment = [[NSProcessInfo processInfo] environment];
            if (environment[@"XCTestConfigurationFilePath"] != nil && ((NSString *)environment[@"XCTestConfigurationFilePath"]).length > 0) {
                runningTests = true;
            } else {
                runningTests = false;
            }
        });
        return runningTests;
    }
    
    0 讨论(0)
  • 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)

    0 讨论(0)
提交回复
热议问题