上一篇文章iOS自動(dòng)化單元測(cè)試之Xcode自帶工具xcodebuild與xccov仰剿,已將講了用xcode自帶工具進(jìn)行單元測(cè)試,但是存在一些問(wèn)題,測(cè)試結(jié)果在終端,可視化太差瑞躺,而且拿不到解析的json文件闸与。接下來(lái)我們講解一個(gè)自動(dòng)化單元測(cè)試的工具fastlane。
一屎慢、環(huán)境檢查:
1环肘、檢查ruby版本
查看本機(jī)已安裝的ruby版本號(hào)复哆,要求版本>2.0
$ ruby -v
顯示
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
2、使用rvm管理ruby版本
安裝rvm $ curl -L https://get.rvm.io | bash -s stable
使用rvm
$ source /Users/xiaonan/.rvm/scripts/rvm
列出ruby可安裝版本列表
$ rvm list known
安裝ruby 2.5.1
$ rvm install 2.5.1
安裝過(guò)程中會(huì)檢查系統(tǒng)是否有安裝Homebrew牙咏,按照默認(rèn)路徑安裝Homebrew丁寄,輸入回車鍵
使用ruby 2.5.1
$ rvm use 2.5.1 --default
3伊磺、檢查Xcode的CLT是否安裝
安裝Command Line Tools
$ xcode-select --install
如果沒(méi)有安裝則自動(dòng)安裝屑埋,否則會(huì)顯示一下錯(cuò)誤
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
二、fastlane的安裝
1痰滋、安裝fastlane
$ [sudo] gem install fastlane -NV
查看安裝的fastlane版本
$ fastlane --version
顯示
fastlane installation at path:
/Users/bijietao/.rvm/gems/ruby-2.5.1/gems/fastlane-2.103.1/bin/fastlane
-----------------------------
[?] ??
fastlane 2.103.1
2、在iOS項(xiàng)目中初始化fastlane
首先在終端進(jìn)去項(xiàng)目根目錄
然后初始化fastlane
$ fastlane init
初始化時(shí)出現(xiàn)4個(gè)選項(xiàng)多艇,選4手動(dòng)設(shè)置
1\. ?? Automate screenshots
2\. ????? Automate beta distribution to TestFlight
3\. ?? Automate App Store distribution
4\. ?? Manual setup - manually setup your project to automate your tasks
之后初始化命令會(huì)自動(dòng)執(zhí)行bundle update
下載項(xiàng)目需要的依賴庫(kù)
初始化完成后逻恐,項(xiàng)目的根目錄會(huì)多2個(gè)文件:Gemfile、Gemfile.lock, Gemfile里面定義了該項(xiàng)目的軟件包依賴的相關(guān)事項(xiàng),和Podfile梢莽、Podfile.lock類似
三、xcov的安裝
1奸披、安裝xcov
sudo gem install xcov
顯示
Password:
Fetching: xcov-1.4.3.gem (100%)
Successfully installed xcov-1.4.3
Parsing documentation for xcov-1.4.3
Installing ri documentation for xcov-1.4.3
Done installing documentation for xcov after 0 seconds
1 gem installed
2昏名、項(xiàng)目中添加xcov
xcode9.3不需要添加
xcode9.2需要在Gemfile中添加 gem "xcov"
四、執(zhí)行fastlane自動(dòng)化測(cè)試
1阵面、項(xiàng)目中需先創(chuàng)建xcworkspace轻局,file -> new ->workspace,創(chuàng)建好后將.xcodeproj 拖到xcworkspace下,否則后面執(zhí)行時(shí)命令時(shí)會(huì)報(bào)下面的錯(cuò)
[18:22:18]: ? xcodebuild: error: 'BJTUnitTestsDemo.xcworkspace' does not exist.
+------------------------------+--------------+
| Lane Context |
+------------------------------+--------------+
| DEFAULT_PLATFORM | ios |
| PLATFORM_NAME | ios |
| LANE_NAME | ios unittest |
| XCODEBUILD_DERIVED_DATA_PATH | |
+------------------------------+--------------+
[18:22:18]: Exit status of command 'set -o pipefail && xcodebuild test -scheme "BJTUnitTestsDemo" -workspace "BJTUnitTestsDemo.xcworkspace" -destination "platform=iOS Simulator,name=iPhone 8 Plus,OS=11.3" | tee '/Users/bijietao/Library/Logs/fastlane/xcbuild/2018-09-13/61665/xcodebuild.log' | xcpretty --color --test' was 66 instead of 0.
xcodebuild: error: 'BJTUnitTestsDemo.xcworkspace' does not exist.
2样刷、在Fastfile中添加下面的腳本代碼
1)xcodebuild說(shuō)明可參考上一篇文章:iOS自動(dòng)化單元測(cè)試之Xcode自帶工具xcodebuild與xccov
2)xcov說(shuō)明仑扑,挑選幾個(gè)重要的講解一下xcov的源碼
Key Description Default workspace 項(xiàng)目的workspace project 項(xiàng)目的project scheme 項(xiàng)目的scheme output_directory 覆蓋率輸出路徑 html_report 是否輸出html文件 true markdown_report 是否輸出markdown文件 false json_report 是否輸出json文件 false ignore_file _path 忽略文件的路徑 * 3) Fastfile中的腳本代碼
default_platform(:ios)
platform :ios do
desc "自動(dòng)化測(cè)試"
lane :unittest do
UI.message("start xcodebuild")
xcodebuild(
test: true,
scheme: "BJTUnitTestsDemo",
workspace: "BJTUnitTestsDemo.xcworkspace",
destination: "platform=iOS Simulator,name=iPhone 8 Plus,OS=11.3"
)
UI.message("success xcodebuild")
UI.message("start xcov")
xcov(
workspace: "BJTUnitTestsDemo.xcworkspace",
scheme: "BJTUnitTestsDemo",
html_report: "true",
json_report: "true",
output_directory:"BJTUnitTests"
)
UI.message("success xcov")
desc "讀取覆蓋率"
file = File.read(File.expand_path("/Users/bijietao/Desktop/BJTUnitTestsDemo/BJTUnitTests/report.json"))
if file
data = JSON.parse(file)
UI.message(data)
UI.message(data["coverage"])
else
UI.error("Unable to open file!")
return
end
end
end
3、終端進(jìn)去項(xiàng)目根目錄 執(zhí)行 fastlane unittest
1)可以看到工程目錄下多了一個(gè)文件
2)終端的效果
3)html文件的效果