XCUITest - QQ群445056799
充分利用Xcodebuild來完善、掌控、加速你的iOS UI自動(dòng)化測(cè)試吧!
Xcode8之前,想要運(yùn)行單元測(cè)試和UI測(cè)試,都需要進(jìn)行編譯。事實(shí)上集成到自動(dòng)化環(huán)境中,在Jenkins節(jié)點(diǎn)等服務(wù)機(jī)上編譯整個(gè)項(xiàng)目(就像打包app)是十分耗時(shí)且依賴配置的。
Xcode8的發(fā)布,帶來了iOS測(cè)試和開發(fā)的xcodebuild的一些新特性:
build-for-testing ?& test-without-building
就是把UITest的編譯和運(yùn)行測(cè)試環(huán)節(jié)可以分開,不僅如此,編譯的結(jié)果可以帶到其它機(jī)器上直接執(zhí)行測(cè)試流程哦。
當(dāng)你的UITest腳本趨于穩(wěn)定的時(shí)候,就可以考慮并發(fā)測(cè)試?yán)?。build once,test anywhere!
2.1編譯(創(chuàng)建xctestrun文件)
xcodebuild build-for-testing
-workspace PreciousMetals.xcworkspace
-scheme PreciousMetalsDevUITests
-destination 'platform=iOS,name=李鵬 SuperXperia'
-derivedDataPath /Users/lipeng/Desktop/DerivedPath
該命令會(huì)編譯app并在DerivedPath文件夾中生成xctestrun文件。
可以在DerivedPath這個(gè)文件夾中找到xctestrun文件
2.2直接執(zhí)行UI自動(dòng)化測(cè)試
xcodebuild test-without-building
-xctestrun /Users/lipeng/Desktop/Build/Products/PreciousMetalsDevUITests_iphoneos12.1-arm64e.xctestrun
-destination 'platform=iOS,name=iPhone 7'
-resultBundlePath /Users/lipeng/Desktop/checkReport
destination參數(shù)可以指定多個(gè)
類似:
xcodebuild \
? ? test-without-building? \
? ? -workspace PreciousMetals.xcworkspace? \
? ? -scheme PreciousMetalsDevUITests? \
? ? -destination "platform=iOS Simulator,name=iPhone X" \
? ? -destination "platform=iOS Simulator,name=iPhone 8 Plus" \
? ? -destination "platform=iOS Simulator,name=iPhone 6s" \
? ? -resultBundlePath /Users/lipeng/Desktop/TestResult/GuijinshuDev
執(zhí)行該命令會(huì)看到命令行終端給出提示:
Testing on multiple destinations concurrently.
Note that it is not possible to run tests in parallel on individual destinations in this mode.
Pass-disable-concurrent-destination-testinginstead to allow tests to run in parallel on each destination, but have the destinations run serially.
Maximum concurrent test device destinations is unlimited. (Adjust this with -maximum-concurrent-test-device-destinations.)
Maximum concurrent test simulator destinations is set to 4. (Adjust this with -maximum-concurrent-test-simulator-destinations.)
默認(rèn)是并發(fā)地使用多個(gè)目標(biāo)來展開測(cè)試。
增加-disable-concurrent-destination-testing參數(shù)可以是測(cè)試在這些目標(biāo)上串行進(jìn)行。
最大的真機(jī)并發(fā)數(shù)量并沒有限制。
真機(jī)和模擬器的最大并發(fā)測(cè)試數(shù)量都可以通過對(duì)應(yīng)的參數(shù)來指定。
-maximum-concurrent-test-device-destinations
-maximum-concurrent-test-simulator-destinations
那么resultBundlePath指定的路徑就是測(cè)試結(jié)果數(shù)據(jù)所在的文件夾啦。
結(jié)合xchtmlreport命令可以產(chǎn)生多設(shè)備測(cè)試結(jié)果報(bào)告。(點(diǎn)擊可以打開項(xiàng)目地址)
在執(zhí)行xcodebuild test或者test-without-building命令的時(shí)候,修改默認(rèn)的-resultBundlePath 參數(shù),將測(cè)試記錄指定到自己想要的目錄yourDir(上文中就是/Users/lipeng/Desktop/TestResult/GuijinshuDev)
安裝:
$ brew install https://raw.githubusercontent.com/TitouanVanBelle/XCTestHTMLReport/develop/xchtmlreport.rb
使用:
$ xchtmlreport -r?/Users/lipeng/Desktop/TestResult/GuijinshuDev
就可以得到更加適合非專業(yè)人士閱讀的UI自動(dòng)化測(cè)試報(bào)告了:

極力推薦這個(gè)測(cè)試報(bào)告生成工具,可以查看多個(gè)機(jī)型的匯總報(bào)告。(相對(duì)于xcpretty更適合生成xcuitest報(bào)告)