Unit Testing in Xcode, does it run the app?

后端 未结 10 962
陌清茗
陌清茗 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:01

    The application is actually run but there is a trick you can use to prevent it from running.

    int main(int argc, char* argv[]) {
        int returnValue;
    
        @autoreleasepool {
            BOOL inTests = (NSClassFromString(@"SenTestCase") != nil
                         || NSClassFromString(@"XCTest") != nil);    
    
            if (inTests) {
                //use a special empty delegate when we are inside the tests
                returnValue = UIApplicationMain(argc, argv, nil, @"TestsAppDelegate");
            }
            else {
                //use the normal delegate 
                returnValue = UIApplicationMain(argc, argv, nil, @"AppDelegate");
            }
        }
    
        return returnValue;
    }
    

提交回复
热议问题