在 TF 中編寫主機端無裝置測試

本頁說明如何編寫不需要裝置的主機端測試,例如在 Linux GCE 執行個體上執行的測試。(如要進一步瞭解如何編寫需要裝置的以主機為導向的測試,請參閱「 在 Trade Federation 中編寫以主機為導向的測試」一文)。

主機端測試類型

您可以透過 Trade Federation (TF) 執行多種主機端測試。

原生 (gtest) 測試

建立原生測試 (gtests) 來測試平台。如果測試不需要裝置,請在主機上執行,這樣測試速度會快很多。如要設定這類測試,使其在測試主機上執行,請使用 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 並啟用自動測試設定產生功能,請在 Android.bp 中將 host_supported 設為 true,如 hello_world_test 所示。

如要進一步瞭解如何編寫原生測試,請參閱「新增原生測試範例」。

JAR 主機測試

JAR (Java) 主機測試 (例如 JUnit) 無須在裝置上執行,可提供 Java 專案的程式碼涵蓋範圍。這類測試可設定為使用執行器 HostTest 在測試主機上執行。

TradeFed 測試設定範例

<configuration description="Executes HelloWorldHostTest">
    <test class="com.android.tradefed.testtype.HostTest" >
        <option name="jar" value="HelloWorldHostTest.jar" />
    </test>
</configuration>

測試設定會執行 HelloWorldHostTest 的主機端 JUnit 測試。請注意,上述測試設定可以自動產生。除非測試需要特殊設定或清除作業,否則請依賴自動測試設定產生功能,建立適當的 TradeFed 測試設定。

如要進一步瞭解如何編寫 JAR 主機測試,請參閱「JAR (Java) 主機測試」頁面。

獨立的 Java 主機測試

您可以在隔離環境中執行不需裝置的 Java 測試,但效能會稍微降低。不過,選擇使用這個環境前,請先考量幾項重大事項。

  • 這是 Robolectric 和 JUnit 單元測試使用的預設執行器
  • Tradefed 僅支援隔離環境中的 JUnit 測試。
  • 系統僅支援靜態連結的依附元件。類別路徑中不包含以 lib 宣告的任何依附元件。
  • 隔離執行器只會將墊片執行器和測試 JAR 放在類別路徑上。
  • 使用這個執行工具執行的每次測試,都會產生固定額外負擔。

Tradefed 測試設定範例 (獨立)

<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 偏好特定 Java 版本,可能與主機系統偏好的 JVM 不一致。
  • exclude-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 執行的 par 檔案。

TradeFed 測試設定範例

<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"],

使用這項設定時,測試會封裝至 test_suites 目標的 general-tests.zip