iOS自動(dòng)化單元測(cè)試之fastlane中xcodebuild與xcov(二)

上一篇文章iOS自動(dòng)化單元測(cè)試之Xcode自帶工具xcodebuild與xccov,已將講了用xcode自帶工具進(jìn)行單元測(cè)試,但是存在一些問題,測(cè)試結(jié)果在終端,可視化太差,而且拿不到解析的json文件。接下來我們講解一個(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

安裝過程中會(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

如果沒有安裝則自動(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)目需要的依賴庫

初始化完成后,項(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說明可參考上一篇文章:iOS自動(dòng)化單元測(cè)試之Xcode自帶工具xcodebuild與xccov
2)xcov說明,挑選幾個(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è)文件
4_3_1.png
2)終端的效果
4_3_2.png
3)html文件的效果
4_3_3.png

五、參考資料

fastlane官網(wǎng)地址 (有詳細(xì)教程)

fastlane源碼

xcov的源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容