Using an alternate AppDelegate for testing in Xcode

One of the first things that happens when unit testing in Xcode is a call to the AppDelegate. This isn’t the end of the world, but it would be nice to bypass any setup done in the production AppDelegate during testing. There are a number of ways to accomplish this, but this article outlines a simple method for using a separate TestingAppDelegate.

In short, change your main.m to:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TestingAppDelegate.h"

int main(int argc, char *argv[])
    @autoreleasepool {
        BOOL isRunningTests = NSClassFromString(@"XCTestCase") != nil;
        Class appDelegateClass = isRunningTests ? [TestingAppDelegate class] :[AppDelegate class];
        return UIApplicationMain(argc, argv, nil, NSStringFromClass(appDelegateClass));

This checks for the presence of an XCTestCase class and calls the appropriate AppDelegate class accordingly.

The TestingAppDelegate class can be as minimal as follows, or you can configure it any way you like. (Note: Be sure to add the TestingAppDelegate to the app target, not the testing target)


#import <UIKit/UIKit.h>

@interface TestingAppDelegate: UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;


#import "TestingAppDelegate.h"

@implementation TestingAppDelegate

That’s it! Now you can configure as much or as little as you like before running tests.

One thought on “Using an alternate AppDelegate for testing in Xcode

  1. nice blog too informative. looking and reading your points its so impressive. doing more blog like this. i really appreciated doing like this.

Leave a Reply