AndroidTest.xml 結構

模組設定的整體結構與一般 Tradefed XML 設定類似,但由於模組是套件的一部分,因此會受到一些限制。

允許的代碼清單

AndroidTest.xml 或更廣泛的模組設定只能包含下列 XML 標記:target_preparermulti_target_preparertestmetrics_collector

雖然這份清單看起來限制重重,但可讓您精確定義測試模組設定需求和要執行的測試。

注意:如需不同標記的複習資料,請參閱「Tradefed XML 設定」。

如果嘗試從模組設定內執行 build_providerresult_reporter 等物件,系統會引發 ConfigurationException。這是為了避免這些物件實際在模組內執行某些工作。

模組設定範例

<configuration description="Config for CTS Gesture test cases">
    <option name="test-suite-tag" value="cts" />
    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="CtsGestureTestCases.apk" />
    </target_preparer>
    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="android.gesture.cts" />
        <option name="runtime-hint" value="10m50s" />
    </test>
</configuration>

這項設定說明需要安裝 CtsGestureTestCases.apk 的測試,並會針對 android.gesture.cts 套件執行檢測。

包含標記 <include><template-include>

不建議在模組設定中使用 <include><template-include>。無法保證能正常運作。

metrics_collector 標記的特殊情況

允許使用 metrics_collector,但僅限於 FilePullerLogCollector 類別,以便指定要擷取並記錄的模組檔案或目錄。如果您將記錄檔留在特定位置,並想自動復原這些記錄檔,這個方法就十分實用。

設定範例如下:

<configuration description="Config for CTS UI Rendering test cases">
    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="cleanup-apks" value="true" />
        <option name="test-file-name" value="CtsUiRenderingTestCases.apk" />
    </target_preparer>
    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="android.uirendering.cts" />
        <option name="runtime-hint" value="11m55s" />
        <option name="runner" value="android.uirendering.cts.runner.UiRenderingRunner" />
        <option name="isolated-storage" value="false" />
    </test>

    <!-- Collect the files in the dump directory for debugging -->
    <metrics_collector class="com.android.tradefed.device.metric.FilePullerLogCollector">
        <option name="directory-keys" value="/sdcard/UiRenderingCaptures" />
        <option name="collect-on-run-ended-only" value="true" />
    </metrics_collector>
</configuration>

建構資訊或下載內容呢?

允許使用的標記定義可能會造成錯誤印象,以為模組不會取得任何建構資訊。這並非事實

建構資訊來自套件層級設定,並由套件的所有模組共用。這樣一來,您只要為套件進行一次頂層設定,就能執行套件中的所有模組。

舉例來說,各個相容性測試套件 (CTS) 模組不必個別查詢裝置資訊、類型等,CTS 套件層級設定 (cts.xml) 只要執行一次,各模組就能在要求時收到該資訊。

如要讓模組中的物件接收建構資訊,必須與一般 Tradefed 設定相同:實作 IBuildReceiver 介面來接收 IBuildInfo。詳情請參閱「使用裝置進行測試」。

中繼資料欄位

大量測試模組包含一些 metadata 規格,每個規格都有獨一無二的目標。

例子:

  <option name="config-descriptor:metadata" key="component" value="framework" />
  <option name="config-descriptor:metadata" key="parameter" value="instant_app" />
  <option name="config-descriptor:metadata" key="parameter" value="multi_abi" />
  <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />

元件

component 中繼資料說明模組要測試的一般 Android 元件。這不會直接影響測試執行作業,主要用於組織用途。

如需 CTS 允許的最新元件清單,請參閱 CtsConfigLoadingTest。如果將不存在的元件新增至 CTS 模組,預先提交測試就會失敗。

您可以使用 module-metadata-include-filtermodule-metadata-exclude-filter,根據元件篩選套件執行作業。

例子:

  --module-metadata-include-filter component framework

這個範例只會執行以 framework 元件註解的測試模組。

參數

parameter 中繼資料僅供參考,但會影響測試執行。指定測試模組適用的 Android 模式。在這種情況下,模式僅限於高階 Android 模式,例如 instant appssecondary usersdifferent abis

在套件執行期間,如果模式適用於測試,系統會根據模式建立測試模組的數個變體。每個變數都會執行類似的測試,但模式不同。

  • instant_app:建立測試變體,將 APK 安裝為免安裝應用程式。
  • multi_abi:為裝置支援的每個 ABI 建立測試變體。
  • secondary_user:建立測試變體,安裝 APK 並以次要使用者身分執行測試。

收集指標並後續處理效能測試模組

對於效能測試模組,允許使用模組層級的 metrics_collectormetric_post_processor,因為這些是效能測試的必要條件。模組層級的指標收集器和後處理器可以專屬於模組。不建議在頂層和模組層級同時指定後處理器。

效能測試模組設定必須包含 test-type 中繼資料,且值為 performance,例如: xml <option name="config-descriptor:metadata" key="test-type" value="performance" /> 否則,如果測試設定包含 metric_collector (FilePullerLogCollector 或任何 metric_post_processor 除外),測試會在預先提交時失敗。

效能測試模組設定範例:

<configuration description="Runs sample performance test.">
    <!-- Declare as a performance test module -->
    <option name="config-descriptor:metadata" key="test-type" value="performance" />
    <option name="test-tag" value="hello-world-performance-test" />
    <test class="com.android.tradefed.testtype.HostTest" >
        <option name="class" value="android.test.example.helloworldperformance.HelloWorldPerformanceTest" />
    </test>
    <!-- Add module-level post processor MetricFilePostProcessor -->
    <metric_post_processor class="com.android.tradefed.postprocessor.MetricFilePostProcessor">
        <option name="aggregate-similar-tests" value="true" />
        <option name="enable-per-test-log" value="false" />
    </metric_post_processor>
</configuration>