有没有办法在应用程序和正在运行的UI测试之间共享实例/数据?我的理解是,它们作为两个不同的应用程序运行,这使得它不可能实现,但我想检查一下。我在想像这样的东西:
// included in both my app and the UI test
class Foo {
let shared: Foo()
var value = ""
}
// In the UI test:
class BasicAccessibility: XCTestCase {
func testFoo() {
Foo.shared.value = "bar"
}
}
// In the app
class FooController: UIViewController {
override func viewDidLoad() {
label.value = Foo.shared.value
}
}我试图从UI测试中模拟设备陀螺仪,因此在我的示例中,Foo将是某个陀螺仪管理器实例。我最好的选择是在应用程序中包含一个测试可以交互的UI元素,这是很糟糕的。
===编辑===
我不认为This question是相似的,因为它不是关于UI测试的,并且UI测试应用程序具有与普通应用程序不同的设置和功能。
发布于 2019-05-30 14:02:25
我不知道这是不是一个非常好的想法,但这是我正在尝试的东西,而且似乎效果很好。如果有人知道这么做的一个非常糟糕的原因,请让我知道。
创建一个名为Person.swift的文件,并将其添加到应用程序目标和UITestTarget中。
struct Person: Codable {
let name: String
let age: Int
let height: Int
let weight: Int
}在您的UITests中“序列化”对象并通过启动环境字典发送它。
class UITestingDataUITests: XCTestCase {
let app = XCUIApplication()
override func setUp() {
continueAfterFailure = false
let person = Person(name: "Tom Cruise", age: 56, height: 170, weight: 60)
let json = String(data: try! JSONEncoder().encode(person), encoding: .utf8)
continueAfterFailure = false
app.launchArguments.append("isUITesting")
app.launchEnvironment["custom-object-person"] = json
app.launch()
}在你的应用委托中访问对象:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let personInfo = ProcessInfo.processInfo.environment["custom-object-person"]!
let person = try! JSONDecoder().decode(Person.self, from: personInfo.data(using: .utf8)!)
print(person)
return true
}https://stackoverflow.com/questions/42633630
复制相似问题