top of page

使用 Fastlane 執行 Xcode 測試 - (1/2)

  • 作家相片: Mason Chang
    Mason Chang
  • 1月7日
  • 讀畢需時 2 分鐘

已更新:1月23日

自動化測試是每個 iOS 工程師都應該關注的議題,因為它不僅可以提升專案的開發效率,也能減少人為疏失所帶來的風險。(你舒服且老闆高興)

在這篇文章中,我將分享如何單獨使用 Fastlane,串接 Xcode 進行測試自動化,並詳細介紹常用的參數和最佳實踐。



為什麼選擇 Fastlane?

  • Fastlane:Fastlane 是一個強大的工具,用於自動化處理 iOS 和 Android 的開發工作,例如打包、上傳 App Store,以及執行測試。它簡單易用,並且有豐富的社群支持。



初始化與安裝 Fastlane


  • 使用 Bundler 安裝 Fastlane

    • 為了更好地管理依賴,我們建議使用 Bundler 安裝 Fastlane。

    • 步驟:

      1. 安裝 Bundler

gem install bundler

2. 在專案的目錄下建立 Gemfile:

bundle init

3. 添加 Fastlane 到 Gemfile 內,在 Gemfile 添加以下內容:

gem "fastlane"

4. 透過 Bundle 安裝 Fastlane:

bundle install

  • 初始化 Fastlane

    • 執行以下指令初始化 Fastlane:

bundle exec fastlane init

Fastlane 將詢問一些問題,例如是否需要配置 App Store 上傳或測試流程,按照需求選擇。

完成後,Fastlane 會生成 Fastfile 和 Appfile 等配置文件。



使用 Fastlane 串接 Xcode 測試

  • 添加測試 Lane

    • 在 Fastfile 中添加以下內容:

lane :run_tests do
  scan(
    scheme: "YourAppScheme",  # 替換為你的 Scheme 名稱
    clean: true,               # 每次測試前清理專案
    device: "iPhone 14",      # 指定測試設備
    output_directory: "./test_results", # 測試結果輸出位置
    output_types: "junit"     # 輸出格式(可選 json、html、junit)
  )
end
  • 常用參數解釋:

    • scheme:你的專案 Scheme 名稱,通常可以在 Xcode 中找到。

    • clean:是否在執行測試前清理專案,預設為 false。

    • device:指定模擬器設備名稱,例如 "iPhone 14" 或 "iPad Pro"。

    • output_directory:測試結果的輸出路徑。

    • output_types:測試結果的格式,可選 json、html 或 junit。


  • 執行測試

    • 在終端執行以下指令進行測試:

bundle exec fastlane run_tests

執行後,Fastlane 將自動開啟指定的模擬器並執行測試,測試結果將保存到 ./test_results 目錄下。



常用參數與最佳實踐

  • 常用參數 (全部參數可看官網)

    • 以下是 scan 常用的參數及其用途:

參數

說明

workspace

指定 Xcode 的 workspace 文件路徑

scheme

測試的目標 Scheme 名稱

device

指定模擬器設備

clean

是否在測試前清理專案

output_types

測試結果格式(json、html、junit)

output_directory

測試結果輸出位置

only_testing

只執行特定測試類別或方法,例如 "Tests/ClassName/testMethod"

skip_build

跳過測試前的編譯階段


 
 
 

留言


follow me
  • Facebook
  • LinkedIn

Thanks for submitting!

IMG_5625_edited_edited.jpg
Mason
Small habits, repeated daily, lead to extraordinary results.
bottom of page