Unit Testing in Xcode, does it run the app?

后端 未结 10 955
陌清茗
陌清茗 2020-11-29 21:54

I\'m running into a strange problem that I haven\'t run into before.

When you do cmd+U to run your Unit Tests (OCUnit for example) does it actually call the main.m,

10条回答
  •  [愿得一人]
    2020-11-29 22:04

    Excellent answers above that suggest dynamically changing the application delegate at run time.

    The small modification I make is to detect a unit test run by querying NSProcessInfo. The advantage is that you don't need to have a class that can be detected to see if unit tests are running.

        int main(int argc, char * argv[])
        {
            // Put your App delegate class here.
            const Class appDelegateClass = [ATAppDelegate class];
    
            NSDictionary *const environmentDictionary =
            [[NSProcessInfo processInfo] environment];
    
            const BOOL runningUnitTests =
            environmentDictionary[@"XCInjectBundleInto"] != nil;
    
            NSString *delegateName = 
            runningUnitTests ? nil : NSStringFromClass(appDelegateClass);
    
            @autoreleasepool {
                return UIApplicationMain(argc, argv, nil, delegateName);
            }
        }
    

    The @"XCInjectBundleInto" property in environmentDictionary is the path to your unit tests bundle and is set up by Xcode.

提交回复
热议问题