I have a UI test like so :
func testHome(){
if(isRedOrange.clear()){
//code
}
}
How would I access my isRedOrange.clear function from my isRedOrange.swift file from my UI tests?
I have a UI test like so :
func testHome(){
if(isRedOrange.clear()){
//code
}
}
How would I access my isRedOrange.clear function from my isRedOrange.swift file from my UI tests?
UI Tests are black boxed, so you cant have access to your code.
You can use @testable import
in Unit Tests, so full access will be provided.
When you're running UITests this is not working, because during a UITest your test class cannot access your app's code.
From Apple's Docs:
UI testing differs from unit testing in fundamental ways. Unit testing enables you to work within your app's scope and allows you to exercise functions and methods with full access to your app's variables and state. UI testing exercises your app's UI in the same way that users do without access to your app's internal methods, functions, and variables. This enables your tests to see the app the same way a user does, exposing UI problems that users encounter.
You must achieve everything using .tap()
's on elements. .accessibilityIdentifier
will help you to get the right element
Goto projects settings -> Select uitests target -> build phases tab -> add your swift file to compile sources
You need to import your main module (project) into tests:
ENABLE_TESTABILITY
in Build Settings
of the main project target to true.@testable import MAIN_TARGET_NAME
in your UITests file.@testable import MAIN_TARGET_NAME
be located at? below import UIKit? Also, upon doing that my class is not showing –
Updraft No such module 'InternetAvailabilty.swift'
–
Updraft UIKit
using import UIKit
, you will use @testable import YOUR_APP
to import your main target. Also, if you just have a global function func clear()
, you just call it with clear()
, you never call the filename in Swift –
Regulator The question makes no sense. You don’t access any of your app’s code in a UI test. If you want to access your code, write a unit test.
NSOpenPanel
to provide a consistent output in UI tests, so I defined a protocol and extended NSOpenPanel
to adopt it. I also defined a mock class in my UI Test target that also adopts the protocol. The protocol definition needs to be available to both the app and the UI tests. I just moved the protocol to its own source file and set its "Target Membership" to both app and UI tests. –
Transmigrate NSOpenPanel
or mock its behavior and returned URL). The app code really is inaccessible from the UI Test target. Makes perfect sense though, since UI tests are based on macOS's accessibility features... –
Transmigrate © 2022 - 2024 — McMap. All rights reserved.