![](/img/trans.png)
[英]How can I call methods in app code from XCTestCase (UI Test Case)
[英]Access to project code from XCTestCase - UI Test
我正在嘗試在我的項目中設置UI測試。 我正在進行UI測試,試圖通過我的應用程序登錄提示登錄。 為了確保在測試啟動時顯示登錄提示,我正在嘗試運行項目代碼庫中的ServerManager.logout()
。 這將導致在啟動時顯示登錄提示。
import XCTest
class SmokeTest: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
ServerManager.logout()
}
func testLogin() {
let app = XCUIApplication()
let emailTextField = app.textFields["email textfield"]
emailTextField.tap()
emailTextField.typeText("test@test.com")
let passwordTextField = app.secureTextFields["password textfield"]
passwordTextField.tap()
passwordTextField.typeText("12345678")
let loginButton = app.buttons["log in button"]
loginButton.tap()
}
}
我應該如何設置項目以訪問ServerManager
?
當我在ServerManager.swift文件上檢查目標成員資格的UITest目標(稱為DonkeyProductionUITests)時,Xcode開始抱怨該文件中的許多引用未定義為UITest目標。 所以我將項目中所有文件的目標成員資格添加到UITest目標,包括所有CocoaPods。 它解決了大多數問題。 我還有一些奇怪的剩菜:
UITest目標應該具有哪些源文件作為“編譯源”?
為什么UIColor
和UIViewController
等類型突然無法通過Xcode識別?
通過@testable import
訪問項目代碼只能在UnitTests中進行 。 當您運行UITests時,這不起作用,因為在UITest期間,您的測試類無法訪問您的應用程序代碼。
來自Apple的文檔 :
UI測試與基本方式的單元測試不同。 單元測試使您能夠在應用程序范圍內工作,並允許您通過完全訪問應用程序的變量和狀態來練習功能和方法。 UI測試以與用戶相同的方式練習應用程序的UI,而無需訪問應用程序的內部方法,函數和變量。 這使您的測試能夠以與用戶相同的方式查看應用程序,從而暴露用戶遇到的UI問題。
如果您想在測試后注銷,則必須通過應用程序的用戶界面進行注銷:如果您的應用程序中某處有注銷按鈕,請在測試結束時導航並讓測試tap()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.