How do I keep the app open between UITests in Xcode
This question is a bit old, but XCTestCase has a class func for setup()
and tearDown()
that executes before the first test, and after the last test completes. If you create your XCUIApplication()
as a singleton (say XCUIAppManager), you could put your XCUIAppManager.shared.launchApp()
call within the override class func setup()
method... this will only launch the app once per XCTestCase (contains multiple tests)
override class func setUp() { // 1.
super.setUp()
// This is the setUp() class method.
// It is called before the first test method begins.
// Set up any overall initial state here.
}
https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods
Example app manager singleton implementation:
import XCTest
class XCUIAppManager {
static let shared = XCUIAppManager()
let app = XCUIApplication(bundleIdentifier: "com.something.yourAppBundleIdentifierHere")
private(set) var isLaunched = false
private init() {}
func launchApp() {
app.launch()
isLaunched = true
}
}
To improve on @James Goe's answer -
To avoid all those relaunches, but keep the ability to run individual tests - and not care about the order in which they're run - you can:
class MyTests: XCTestCase {
static var launched = false
override func setUp() {
if (!MyTests.launched) {
app.launch()
MyTests.launched = true
}
(Of course, now that you're not relaunching your app you have to care much more about state, but that's another story. It's certainly faster.)
In your setUp()
method, remove [[[XCUIApplication alloc] init] launch];
and put it into the first test you will be performing.
For eg.,
If you have tests: testUI(), testUIPart2(), testUIPart3(), etc., and it runs in this order, put [[[XCUIApplication alloc] init] launch];
into the first line of testUI() and no where else.