Unit Testing in Xcode, does it run the app?
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;}
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 <Foundation/Foundation.h>@interface TestAppDelegate : NSObject<UIApplicationDelegate>@end
TestAppDelegate.m
#import "TestAppDelegate.h"@implementation TestAppDelegate@end
In Swift, I prefere to bypass a normal execution path inside application: didFinishLaunchingWithOptions
:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { guard normalExecutionPath() else { window = nil return false } // regular setup return true}private func normalExecutionPath() -> Bool { return NSClassFromString("XCTestCase") == nil}
Code inside guard
will remove any views created from storyboard.