2017-02-13 42 views
3

我想在我的項目中設置UI測試。我正在進行UI測試,嘗試通過我的應用程序登錄提示登錄。爲了確保在測試啓動時顯示登錄提示,我試圖運行ServerManager.logout(),它位於項目的代碼庫中。這將導致在啓動時顯示登錄提示。從XCTestCase訪問項目代碼 - UI測試

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("[email protected]") 

     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目標。因此,我將項目中所有文件的Target Membership添加到UITest Target,包括所有CocoaPods。它解決了大部分問題。不過我有一些奇怪的剩菜:

Undefined reference

應UITest目標必須爲「編譯源代碼」是什麼源文件?

爲什麼類型,如UIColorUIViewController突然不Xcode的識別器?

回答

2

通過@testable import訪問您項目的代碼僅在UnitTests中可能。當你運行UITests這是行不通的,因爲在UITest期間你的測試類不能訪問你的應用程序的代碼。

Apple's Docs來自:

UI測試不同於基本方式的單元測試。單元測試 使您能夠在應用程序的範圍內工作,並允許您使用完全訪問應用程序變量和 狀態的功能和方法。 UI測試以與用戶 無需訪問應用程序的內部方法,函數和 變量的相同方式來執行應用的用戶界面。這使您的測試能夠以與用戶 相同的方式查看應用,從而暴露用戶遇到的UI問題。

如果你願意,你必須通過應用程序的用戶界面做一個測試後退出:如果有一個註銷按鈕的地方在您的應用程序,在您的測試結束導航那裏,讓測試tap()它。

+0

太棒了,謝謝!但有點惱人..我只是想確保應用程序在測試啓動時處於正確的狀態,但這似乎不是'func setup()'的用例...您是否介意添加鏈接來自蘋果的參考? – Wiingaard

+0

是的,這有點不方便,但是沒有辦法。測試後,您必須使用應用的用戶界面重置您的應用。如果你需要在每次測試後做到這一點,你可以將該代碼移動到'tearDown()'。這裏是鏈接到文檔:https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html – joern

+0

你不覺得這是一個解決方案啓動應用程序的參數,像這樣的解決方案:http://stackoverflow.com/a/33466038/2299801謝謝:) – Wiingaard