問題描述
Xcode 7? 提供了一種直接測試 UI 的新方法,包括新的測試目標iOS UI Testing Bundle"(或OS X").
Xcode 7? has a new way to test your UI directly, including a new testing target "iOS UI Testing Bundle" (or "OS X").
在 UI 測試目標中,似乎沒有對構成您的應用的模型或類的內置訪問.例如.[UIApplication sharedApplication]
不能從您的 UI 測試中調用.這意味著應用程序測試"和UI 測試"存在于一個可能無法逾越的鴻溝中.
In the UI testing target, it appears there's no built-in access to the model or classes that comprise your App. E.g. [UIApplication sharedApplication]
would not be callable from your UI tests.
This implies that "app tests" and "UI tests" exist across a possibly unbridgeable chasm.
如上所述這里:
問題是 Xcode 的 UI 測試不允許訪問實際的應用程序.
The problem is that Xcode’s UI testing does not allow access to the actual app.
問題:
- 這個鴻溝可以彌合嗎?如果是這樣,詳細說明如何使用構建和鏈接器設置以及可能在 github 上工作的
xcodeproj
. - 可以在 Apple 文檔中的何處找到關于這種分歧的明確聲明.
<小時>
? 在撰寫本文時,測試版軟件.
? At the time of writing, beta software.
推薦答案
黑盒測試
UI 測試是一個黑盒測試框架.您不必對正在測試的代碼的實現有任何了解.
UI Testing is a black-box testing framework. You shouldn't have to know anything about the implementation of the code you are testing.
例如,您應該只關心標簽上的值發生變化,而不是控制器將正確的數據傳遞給視圖.您可以從應用用戶的角度來考慮 UI 測試.她不在乎您的 ItemsViewController
是如何工作的(甚至它是否存在),那么為什么要進行 UI 測試呢?
For example, you should only care that the value on a label changes, not that the controller passed the right data to the view. You can think of UI Testing from the user of your app's perspective. She doesn't care how your ItemsViewController
works (or even that it exists), so why should the UI Tests?
讓它工作"
話雖如此,我理解你的沮喪.如果您可以啟動一個視圖控制器,然后使用 UI 測試并做出斷言,那就太好了.但是,從 Beta 5 開始,這是不可能的.
That being said, I understand your frustration. It would be great if you could spin up a view controller and then tap around with UI Testing and making assertions. However, as of Beta 5 this is not possible.
有趣的是,您可以在 UI 測試頂部使用簡單的 @testable import ModuleName
創建應用對象的實例.請注意,您實際上無法通過類似 .tap()
的方法與它進行交互,因為它是一個 UI*
類,而不是 XCUI*代碼> 一個.
What's interesting is that you can, however, create instances of your app's objects with a simple @testable import ModuleName
at the top of your UI Tests. Note that you can't actually interact with it via the .tap()
-like methods, as it's a UI*
class, not a XCUI*
one.
將 Donut
視為應用程序的模塊名稱.
Consider Donut
to be the application's module name.
import XCTest
@testable import Donut
class DonutUITests: XCTestCase {
let app = XCUIApplication()
override func setUp() {
continueAfterFailure = false
app.launch()
}
func testItemsViewController() {
let controller = ItemsViewController()
controller.addItemButton.tap() // <---- UIButton does not respond to tap()!
}
}
這篇關于Xcode 7:應用程序測試和 UI 測試之間的鴻溝是不可逾越的嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!