Xcode: TEST vs DEBUG preprocessor macros

ⅰ亾dé卋堺 提交于 2019-11-26 21:56:39

Preprocessor macros will not work, you need to check the environment at runtime.

static BOOL isRunningTests(void)
{
    NSDictionary* environment = [[NSProcessInfo processInfo] environment];
    return (environment[@"XCInjectBundleInto"] != nil);
}

(Updated for Xcode 7.3)

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.

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.

focused4success

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 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.

Robert's answer in SWIFT 3.0:

func isRunningTests() -> Bool {
    let environment = ProcessInfo().environment
    return (environment["XCInjectBundleInto"] != nil);
}
regrecall

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.

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
}

Updated for Xcode10:

static let isRunningUnitTests: Bool = {
    let environment = ProcessInfo().environment
    return (environment["XCTestConfigurationFilePath"] != nil)
}()

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;
}

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!