Unit Testing in Xcode, does it run the app?

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

    I found another solution to the problem:

    int main(int argc, char * argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, ({
                ![NSProcessInfo processInfo].environment[@"XCTestConfigurationFilePath"] ?
                @"AppDelegate" :
                nil;                
            }));
        }
    }
    

    From here: http://qualitycoding.org/app-delegate-for-tests/#comment-63984

    0 讨论(0)
  • 2020-11-29 22:12

    Using xCode 7 and xCtool

    xctool is capable of executing unit tests without running the app.

    To get this working,

    1 . Update target settings to run without a host app.

    Select your project --> then test target --> Set the host application to none.

    2. Install xctool , if you don't have it.

    brew install xctool
    

    3. Run the tests using terminal with xctool.

    xctool -workspace yourWorkspace.xcworkspace -scheme yourScheme run-tests -sdk iphonesimulator
    
    0 讨论(0)
  • 2020-11-29 22:12

    You can do that by setting the Host Application to None in your Tests Target.

    0 讨论(0)
  • 2020-11-29 22:16

    I use the approach of Tomasz Bak plus some code of dwb answer and come up with the following:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
        BOOL runningTests = NSClassFromString(@"XCTestCase") != nil;
        if (runningTests) {
            self.window.rootViewController = [UIViewController new];
            return true;
        }
    
        // Your normal code below this
        ....
    }
    
    0 讨论(0)
提交回复
热议问题