Due to a couple issues, I want the XCTest target in a project to run a separate app delegate. Using ObjC, this was a relatively straightforward process: manipulate main.m<
It's strongly unrecommended to add conditions to normal code checking if its being tested. Instead you should mock your AppDelegate
in tests to do whatever you want.
Then you could replace delegate of UIApplication is setUp
in super class of your each XCTestCase
'es.
class MockAppDelegate:NSObject, UIApplicationDelegate {
}
class BaseTest: XCTestCase {
override func setUp() {
super.setUp()
UIApplication.shared.delegate = MockAppDelegate()
}
}
class Test1: BaseTest {
override func setUp() {
super.setUp()
// normal testing
}
}
If you still want to stop code execution for tests this is my method that works well:
You can add startup parameter to app which indicates that this is test run
These parameters are accessible from NSUserDefaults
#define IS_TESTS [[NSUserDefaults standardUserDefaults] boolForKey:@"TESTING"]