How to import my App module into MyAppUITests file?
Asked Answered
U

5

17

This is my simple test case:

import XCTest
@testable import MyApp //it doesn't work

because of this:

enter image description here

class TabBarControllerTests: XCTestCase {

    override func setUp() {
        super.setUp()

        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setObject([], forKey: DBTabBarOrderedIndexesKey) //key is undefined, because of lack of my app module
        defaults.synchronize()

        continueAfterFailure = false
        XCUIApplication().launch()
    }

    func testIsOrderOfTabsSaved() {

        XCUIApplication().tabBars.buttons["Catering"].tap()
        //what next?
    }
}

Once I tap UITabBarItem I change the value of DBAppSettings.mode, so here I would like to have an access to my DBAppSettings.mode property to check if it is really changed.

I noticed that there is one weird thing, when I build my app, and check what was built, there is no build for my UITest target. Is it important?

enter image description here

Untouchable answered 2/8, 2015 at 6:16 Comment(0)
U
10

This is a response from Apple:

UI tests execute differently from Unit tests - Unit tests run inside your application process so they can access your application code. UI tests execute in a separate process, outside your application, so they can simulate how the user interacts with the application. It’s not expected that you will be able to access your app class from a UI test.

Untouchable answered 16/8, 2015 at 20:58 Comment(0)
O
3

Rather than having your tests know about your app, try turning it around and have your app know that it's being tested. One way is to use the launchArguments property:

    app = XCUIApplication()
    app.launchArguments.append("TestMode")
    app.launch()

Then in your app:

    if NSProcessInfo.processInfo().arguments.contains("TestMode") {
        // I am running in test mode
    }

In your case the app could then set the NSUserDefaults accordingly.

Oyler answered 26/12, 2015 at 6:41 Comment(0)
M
2

Every object you need access to in UI Tests must be part of the UI Test target. This includes object dependencies. It's a slippery slope, and rather a mess.

Madelaine answered 19/9, 2015 at 4:53 Comment(0)
H
1

I solved the inaccessible symbol problem by pulling some stuff from my app target into two frameworks (model and view-model) that I can then import into my UI tests.

As for accessing the actual memory of the executable, you can't, but you can test for things that should appear on screen somewhere in that mode. I use this assertion to e.g. check for the existence of a table view cell:

XCTAssertTrue(tables.cells.staticTexts["Cell title I expect to exist"].waitForExistence(timeout: 1))

Since you can use accessibility you can access a pretty decent amount of things this way. I suppose you could add an invisible label with a dump of your app's memory-in test mode only!

I too make use of the launch arguments to configure my app for UI tests, as Michael suggested: https://mcmap.net/q/119787/-how-to-import-my-app-module-into-myappuitests-file.

Huckaback answered 2/12, 2017 at 5:22 Comment(0)
P
0

Since Apple prevents you from accessing your main application from the UI tests, you might think about reorganizing your application structure to store the relevant data which needs checking into a spot accessible to the UI tests.

You might consider moving definitions and data from your main application class to a separate class which can be loaded by the test framework.

Pontefract answered 23/8, 2015 at 12:7 Comment(4)
It is not a good idea since I need access to my App, not for some classes. In question there is a simple case with key, but in common I need access to some singletons.Nonaligned
I usually put singletons into their own classes external to the app's shared instance. Would that work for your purpose?Pontefract
would work, but objects within these singletons are another objects from my class, so basically I would need to attach to the test target every file from my app target:-) it is not expected...Nonaligned
@BartłomiejSemańczyk were you able to found a solution to access the singleton in UITest file?Bituminous

© 2022 - 2024 — McMap. All rights reserved.