本頁說明如何編寫不需使用 例如在 Linux GCE 執行個體上執行的測試(如要進一步瞭解 要編寫需要裝置的主機導向測試,請參閱 撰寫商業聯盟適用的主機驅動測試)。
主機端測試類型
你可以透過貿易聯盟執行多種主機端測試 (TF)。
原生 (gtest) 測試
建立原生測試 (gtest) 測試平台。如果不需裝置即可執行測試,請在 host;測試執行的速度就會加快許多如何設定這類測試 測試主機,請使用 TF 執行器 HostGTest。
以下是 TradeFed 測試設定範例:
<configuration description="Runs hello_world_test."> <option name="null-device" value="true" /> <test class="com.android.tradefed.testtype.HostGTest" > <option name="module-name" value="hello_world_test" /> </test> </configuration>
測試設定會在主機上執行 gtest 測試 (hello_world_test)。範例測試 設定檔可以自動產生除非測試需要特殊設定或清理 您就能利用自動測試設定產生功能 製作適當的 TF 測試 儲存空間設定
如要設定主機端 gtest 並啟用自動測試設定產生功能,請
host_supported
到 true
英寸
Android.bp
,例如 hello_world_test。
如要進一步瞭解如何編寫原生測試,請參閱 新增原生測試 範例。
JAR 主機測試
JAR (Java) 主機測試,例如 JUnit 是不需要在裝置上執行的測試,而且會提供程式碼 Java 專案的涵蓋率這類測試可設為在測試中執行 方法是使用執行器 HostTest。
交易測試設定範例
<configuration description="Executes HelloWorldHostTest"> <test class="com.android.tradefed.testtype.HostTest" > <option name="jar" value="HelloWorldHostTest.jar" /> </test> </configuration>
測試設定會執行 HelloWorldHostTest 的主機端 JUnit 測試。請注意,上述測試設定 可以自動產生除非您的測試需要特殊設定或清理,否則請 自動產生測試設定功能,以便建立適當的 TradeFed 測試 此外還會從 0 自動調整資源配置 您完全不必調整資源調度設定
如要進一步瞭解如何編寫 JAR 主機測試,請參閱 「JAR (Java) Host Tests」網頁。
隔離的 Java 主機測試
您可以在隔離環境中執行無裝置 Java 測試,以稍微降低效能。 不過,選擇使用這項功能前,請先考量幾個重點 環境。
- 這是 Robolectric 和 JUnit 單元測試使用的預設執行器
- Tradefed 僅支援隔離環境中的 JUnit 測試,
- 僅支援靜態連結的依附元件。未宣告任何依附元件
lib
則包含在類別路徑中。 - 隔離執行器只會將輔助執行器和測試 Jar 放入類別路徑。
- 使用這個執行器執行的每次測試會有一些固定負擔。
交易測試設定範例 (獨立)
<configuration description="Executes HelloWorldHostTest"> <test class="com.android.tradefed.testtype.IsolatedHostTest" > <option name="jar" value="HelloWorldHostTest.jar" /> </test> </configuration>
自動產生的 Soong 設定範例
因此 Soong 可以使用 上述 工具自動產生設定,而無須手動建立測試設定 (如上述)。 需要有如本範例的宣告
java_test_host { name: "HelloWorldHostTest", test_options: { unit_test: true, }, test_suites: ["general-tests"], srcs: ["test/**/*.java"], static_libs: [ "junit", ], }
Robolectric 測試
Robolectric 測試會使用與獨立主機測試相同的執行器,但有一些特殊的 只要設定成「自動重新啟動」 和「在主機維護期間」選項即可
robolectric-resources
選項可啟用一些 Robolectric 專用的指令列選項 傳遞至子程序,並將android-all
的樹狀結構新增至 子程序類別路徑。前述兩種是最佳做法,但必須 執行 Robolectric 測試,結果任何成功。java-folder
選項允許變更子程序使用的 Java 執行階段。這是 因為 Robolectric 偏好使用可能與 主機系統偏好的 JVMexclude-paths
選項可讓子程序執行器避免載入特定模組 這在 JAR 含有可能導致載入錯誤的額外類別時很有用。java.
是常見的排除設定,以避免擲回SecurityException
例外狀況。
Robolectric 設定範例
<configuration description="Executes a Sample Robolectric Test"> <option name="java-folder" value="prebuilts/jdk/jdk9/linux-x86/" /> <option name="exclude-paths" value="java" /> <option name="use-robolectric-resources" value="true" /> <test class="com.android.tradefed.testtype.IsolatedHostTest"> <option name="jar" value="RobolectricExampleTest.jar" /> </test> </configuration>
Robolectric 自動產生作業的 Soong 設定範例
因此您不必手動建立測試設定,例如上述,Soong 可以使用如這個範例的宣告方式,自動產生設定。
android_robolectric_test { name: "HelloWorldRoboTest", srcs: [ "src/**/*.java", ], // Include the testing libraries static_libs: [ "mockito-robolectric-prebuilt", "platform-test-annotations", "testng", "truth-prebuilt", ], instrumentation_for: "HelloWorldApp", }
Python 測試
如果測試邏輯是以 Python 編寫,請使用建構類型 python_test_host
建立可
由 TF PythonBinaryHostTest
執行
交易測試設定範例
<configuration description="Config to run atest unittests"> <test class="com.android.tradefed.testtype.python.PythonBinaryHostTest" > <option name="par-file-name" value="atest_unittests" /> <option name="test-timeout" value="2m" /> </test> </configuration>
測試套件設定
如要讓 TF 存取特定建構作業的主機端測試,請將
將測試模組 `test_suites`
設定設為
`general-tests`
:
test_suites: ["general-tests"],
採用這項設定後,測試會封裝至以下位置上的 general-tests.zip
:
test_suites
目標。