Unit Testing in Xcode, does it run the app?

后端 未结 10 976
陌清茗
陌清茗 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 21:59

    Here's a variation of Sulthan's answer that uses XCTest, which is the default for test classes generated by XCode 5.

    
    int main(int argc, char * argv[])
    {
        @autoreleasepool {
            BOOL runningTests = NSClassFromString(@"XCTestCase") != nil;
            if(!runningTests)
            {
                return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
            }
            else
            {
                return UIApplicationMain(argc, argv, nil, @"TestAppDelegate");
            }
        }
    }
    

    This goes into main.m, which should be under Supporting Files in a standard project layout.

    Then in your tests directory add:

    TestAppDelegate.h

    
    #import 
    
    @interface TestAppDelegate : NSObject
    @end
    

    TestAppDelegate.m

    
    #import "TestAppDelegate.h"
    
    @implementation TestAppDelegate
    @end
    

提交回复
热议问题