Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android CTS 測試指令整理:單一測試方法與整個測試包的執行方式

編輯:關於 Android 程式設計

CTS(Compatibility Test Suite)是 Android 相容性測試套件,以 instrumentation 的方式在裝置上執行。熟悉幾個 adb 指令,就能精準地重跑某個失敗案例,而不必每次跑完整個套件。

先列出裝置上可用的 instrumentation

adb shell pm list instrumentation

輸出會列出每個測試包的執行器名稱與其 target,例如:

instrumentation:com.android.cts.animation/android.test.InstrumentationTestRunner (target=com.android.cts.animation)
instrumentation:com.android.cts.app/android.test.InstrumentationCtsTestRunner (target=com.android.cts.stub)
instrumentation:com.android.cts.os/android.test.InstrumentationCtsTestRunner (target=com.android.cts.stub)

格式是「套件名稱/執行器類別」。後續下指令時必須與這裡的內容完全一致,否則會找不到測試。

執行單一測試方法

用 -e class 指定「測試類別#方法名稱」即可只跑一個案例:

adb shell am instrument -e class android.animation.cts.ValueAnimatorTest#testCurrentPlayTime \
  -w -r com.android.cts.animation/android.test.InstrumentationTestRunner

adb shell am instrument -e class android.app.cts.ActivityManagerMemoryClassTest#testGetMemoryClass \
  -w -r com.android.cts.app/android.test.InstrumentationCtsTestRunner

執行整個測試包

拿掉 -e class 參數,就會執行該套件下的全部測試:

adb shell am instrument -w -r com.android.cts.animation/android.test.InstrumentationTestRunner
adb shell am instrument -w -r com.android.cts.app/android.test.InstrumentationCtsTestRunner

參數意義與延伸用法

-w 代表等待 instrumentation 執行完畢才返回,不加的話指令會立刻結束而看不到結果;-r 則是以原始格式輸出結果,讓每個案例的成敗與堆疊都能被讀取。除錯時常搭配 -e debug true 讓測試進入除錯模式,或用 -e log true 輸出更詳細的測試生命週期日誌。

若要一次篩選多個類別,可以重複傳入多個 -e class 參數。日常開發中,最實用的習慣是先用完整的 CTS 跑出一份失敗清單,再用單一方法指令逐一重跑修驗,能省下大量等待時間。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved