使用命令行工具運行Xcode 7 UI Tests

dyydp 9年前發布 | 20K 次閱讀 Xcode iOS開發 移動開發

使用命令行工具運行Xcode 7 UI Tests

原文:Run Xcode 7 UI Tests from the command line
蘋果在Xcode 7中引入了一項新技術UI Tests,允許開發者使用Swift或Objective C代碼來進行UI測試。直到現在一個可行的方法是使用UI Automation。通過UI Tests,可使用Xcode適當調試問題以及使用Swift或Objective C而無需處理JavaScript代碼。
首先為UI Tests創建一個新的target

使用命令行工具運行Xcode 7 UI Tests

在測試分區下,選中Cocoa Touch UI Testing Bundle:

使用命令行工具運行Xcode 7 UI Tests

現在打開Project UI Tests文件夾中最新創建的Project_UI_Tests.swift文件。在底部你會有一個名為testExample的空方法。注意看圖中的光標,并點擊底部紅色的記錄按鈕。

使用命令行工具運行Xcode 7 UI Tests

這樣就會打開你的app。現在你可以四處點擊下,并與你的應用程序進行交互。完成后,再次點擊紅色錄制按鈕。

使用命令行工具運行Xcode 7 UI Tests

生成的代碼類似下邊的記錄。我已經在生成的代碼行之間添加了幾個示例XCTAsserts。在Xcode中使用CMD + U即可運行你的應用程序,并且是同時運行單元測試和UI Tests。
現在你已經可以使用CLI運行測試了,無需進一步的調整,不過我們想讓UI Tests在一個單獨的scheme中,點擊scheme并選中New Scheme:

使用命令行工具運行Xcode 7 UI Tests

選中最新創建的UI Test target并確認。

使用命令行工具運行Xcode 7 UI Tests

如果你計劃在CI-server上運行測試,請確保最新創建的scheme已經啟用了Shared選項。點擊scheme并選擇Manage Schemes來打開會話。

從CLI打開測

xcodebuild -workspace App.xcworkspace \
   -scheme "SchemeName" \
           -sdk iphonesimulator \
           -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.0'
           test

明確所用Xcode的版本非常重要,我們使用的是最新的測試版本:

export DEVELOPER_DIR="/Applications/Xcode-beta.app"

你甚至可以將測試版作為你新的默認運行版本:

sudo xcode-select --switch "/Applications/Xcode-beta.app"

UI測試運行時終端輸出示例:

Test Suite 'All tests' started at 2015-06-18 14:48:42.601
Test Suite 'TempCam UI Tests.xctest' started at 2015-06-18 14:48:42.602
Test Suite 'TempCam_UI_Tests' started at 2015-06-18 14:48:42.602
Test Case '-[TempCam_UI_Tests.TempCam_UI_Tests testExample]' started.
2015-06-18 14:48:43.676 XCTRunner[39404:602329] Continuing to run tests in the background with task ID 1
    t =     1.99s     Wait for app to idle
    t =     2.63s     Find the "toggleButton" Switch
    t =     2.65s     Tap the "toggleButton" Switch
    t =     2.65s         Wait for app to idle
    t =     2.66s         Find the "toggleButton" Switch
    t =     2.66s         Dispatch the event
    t =     2.90s         Wait for app to idle
    t =     3.10s     Find the "toggleButton" Switch
    t =     3.10s     Tap the "toggleButton" Switch
    t =     3.10s         Wait for app to idle
    t =     3.11s         Find the "toggleButton" Switch
    t =     3.11s         Dispatch the event
    t =     3.34s         Wait for app to idle
    t =     3.54s     Find the "toggleButton" Switch
    t =     3.54s     Tap the "MyButton" Button
    t =     3.54s         Wait for app to idle
    t =     3.54s         Find the "MyButton" Button
    t =     3.55s         Dispatch the event
    t =     3.77s         Wait for app to idle
    t =     4.25s     Tap the "Okay" Button
    t =     4.25s         Wait for app to idle
    t =     4.26s         Find the "Okay" Button
    t =     4.28s         Dispatch the event
    t =     4.51s         Wait for app to idle
    t =     4.51s     Find the "toggleButton" Switch
Test Case '-[TempCam_UI_Tests.TempCam_UI_Tests testExample]' passed (4.526 seconds).
Test Suite 'TempCam_UI_Tests' passed at 2015-06-18 14:48:47.129.
 Executed 1 test, with 0 failures (0 unexpected) in 4.526 (4.527) seconds
Test Suite 'TempCam UI Tests.xctest' passed at 2015-06-18 14:48:47.129.
 Executed 1 test, with 0 failures (0 unexpected) in 4.526 (4.528) seconds
Test Suite 'All tests' passed at 2015-06-18 14:48:47.130.
 Executed 1 test, with 0 failures (0 unexpected) in 4.526 (4.529) seconds
** TEST SUCCEEDED **

生成截圖

無需額外的工作,可輕輕松得到截圖。可為命令添加derivedDataPath選項,你可以告訴Xcode在什么地方儲存測試結果,包括生成的截圖。

xcodebuild -workspace App.xcworkspace \
   -scheme "SchemeName" \
           -sdk iphonesimulator \
           -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.0'
           -derivedDataPath './output'
           test

Xcode會自動為每個測試操作生成截圖,關于生成數據的更多信息,可查看Michele寫的Test Logs in Xcode.

使用命令行工具運行Xcode 7 UI Tests

下一步

想要為多種語言和多種設備類型創建截圖,你可能會需要像snapshot這樣的工具,snapshot使用UI Automation, 不過現在已經棄用了。我目前用的是最新版的snapshot,可充分利用新UI Tests的功能,這樣如果程序出錯,那snapshot能展示更多細節結果和錯誤信息。

來源:FELIX KRAUSE

 本文由用戶 dyydp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!