-
-
Save alcamla/970f241663d0310e76815f0909a1ef25 to your computer and use it in GitHub Desktop.
Detect if a Swift iOS app delegate is running unit tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// Detect if the app is running unit tests. | |
// Note this only detects unit tests, not UI tests. | |
func isRunningUnitTests() -> Bool { | |
let env = NSProcessInfo.processInfo().environment | |
if let injectBundle = env["XCInjectBundle"] { | |
return NSString(string: injectBundle).pathExtension == "xctest" | |
} | |
return false | |
} | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
if(isRunningUnitTests()) { | |
print("TESTING") | |
// Create an empty window. Useful for unit tests that might need to alter the view heirarchy or | |
// add views during their tests. | |
let window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
window.rootViewController = UIViewController() | |
window.makeKeyAndVisible() | |
self.window = window | |
return true | |
} | |
// Perform the normal app init here | |
makeWindow() | |
return true | |
} | |
func makeWindow() { | |
// Do whatever you need here to create the root view controller of the app. | |
let vc = UIStoryboard.init(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("ViewController") | |
let window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
window.rootViewController = vc | |
window.makeKeyAndVisible() | |
self.window = window | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment