Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep the app open between UITests in Xcode

I have a series of UITests I want to run as individual tests but I don't want to relaunch the app between each test. How can I launch the app and keep it open so it doesn't shutdown and restart between tests.

I tried putting XCUIApplication().launch() in init() but got an error.

like image 703
Robert Schmid Avatar asked Dec 15 '15 15:12

Robert Schmid


3 Answers

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.)

like image 54
Adrian Avatar answered Oct 28 '22 01:10

Adrian


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.

like image 7
James Goe Avatar answered Oct 28 '22 01:10

James Goe


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
    }
}
like image 2
Blaine Avatar answered Oct 28 '22 01:10

Blaine