原文: Run Xcode 7 UI Tests from the command line
以及使用Swift或Objective C而无需处理JavaScript代码。
首先为UI Tests创建一个新的target :
在测试分区下,选中Cocoa Touch UI Testing Bundle:
现在打开Project UI Tests文件夹中最新创建的Project_UI_Tests.swift文件。在底部你会有一个名为testExample的空方法。注意看图中的光标,并点击底部红色的记录按钮。
这样就会打开你的app。现在你可以四处点击下,并与你的应用程序进行交互。完成后,再次点击红色录制按钮。
生成的代码类似下边的记录。我已经在生成的代码行之间添加了几个示例XCTAsserts。在Xcode中使用CMD + U即可运行你的应用程序,并且是同时运行单元测试和UI Tests。
现在你已经可以使用CLI运行测试了,无需进一步的调整,不过我们想让UI Tests在一个单独的scheme中,点击scheme并选中New Scheme:
选中最新创建的UI Test target并确认。
如果你计划在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 .
想要为多种语言和多种设备类型创建截图,你可能会需要像 snapshot 这样的工具,snapshot使用UI Automation, 不过现在已经 弃用了 。我目前用的是最新版的snapshot,可充分利用新UI Tests的功能,这样如果程序出错,那snapshot能展示更多细节结果和错误信息。