自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
Tradefed 中的選項處理
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
選項處理是 Trade Federation 模組化方法的核心。具體來說,選項是讓開發人員、整合者和測試執行工具能合作,而不會重複彼此工作的機制。簡單來說,我們實作的選項處理功能可讓開發人員將 Java 類別成員標示為可設定,此時整合器可增強或覆寫該成員的值,並可在之後由 Test Runner 增強或覆寫。此機制適用於所有 Java 內在類型,以及內在類型的任何 Map
或 Collection
例項。
注意:選項處理機制只適用於實作 Test 生命週期中其中一個介面的類別,且只有在該類別由生命週期機制例項化時才有效。
開發人員
首先,開發人員會使用 @Option
註解標示成員。
這些屬性至少會指定 name
和 description
值,這些值會指定與該選項相關聯的引數名稱,以及在使用 --help
或 --help-all
執行指令時,TF 主控台上顯示的說明。
舉例來說,假設我們要建立功能性電話測試,撥打各種電話號碼,並預期在連線後收到每個號碼的一系列 DTMF 音調。
public class PhoneCallFuncTest extends IRemoteTest {
@Option(name = "timeout", description = "How long to wait for connection, in millis")
private long mWaitTime = 30 * 1000; // 30 seconds
@Option(name = "call", description = "Key: Phone number to attempt. " +
"Value: DTMF to expect. May be repeated.")
private Map<String, String> mCalls = new HashMap<String, String>;
public PhoneCallFuncTest() {
mCalls.add("123-456-7890", "01134"); // default
}
開發人員只需設定這兩個測試點,接著,他們可以照常使用 mWaitTime
和 mCalls
,不必特別留意這兩者可設定的事實。由於 @Option
欄位是在類別例項化後,但在呼叫 run
方法之前設定,因此實作者可以輕鬆設定 Map
和 Collection
欄位的預設值,或執行某種篩選作業,否則這些欄位只能附加。
整合服務商
整合器會處理以 XML 編寫的設定。設定格式可讓整合服務供應商為任何 @Option
欄位設定 (或附加) 值。舉例來說,假設整合服務供應商想定義低延遲測試,以呼叫預設數字,以及長時間執行的測試,以呼叫各種數字。他們可以建立一組設定,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration description="low-latency default test; low-latency.xml">
<test class="com.example.PhoneCallFuncTest">
<option name="timeout" value="5000" />
</test>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration description="call a bunch of numbers; many-numbers.xml">
<test class="com.example.PhoneCallFuncTest">
<option name="call" key="111-111-1111" value="#*#*TEST1*#*#" />
<option name="call" key="222-222-2222" value="#*#*TEST2*#*#" />
<!-- ... -->
</test>
</configuration>
測試執行工具
測試執行程式也可以透過 Trade Federation 主控台存取這些設定點。首先,它們會使用 run command <name>
指令 (或簡稱 run <name>
) 執行指令 (也就是設定檔和所有相關引數)。此外,這些物件還可指定任何參數清單做為指令的一部分,以便取代或附加各個設定內生命週期物件指定的欄位。
如要使用 many-numbers
電話號碼執行低延遲測試,測試執行程式可以執行以下操作:
tf> run low-latency.xml --call 111-111-1111 #*#*TEST1*#*# --call 222-222-2222 #*#*TEST2*#*#
或者,為了從相反方向取得類似效果,Test Runner 可以縮短 many-numbers
測試的等待時間:
tf> run many-numbers.xml --timeout 5000
選項排序
您可能會注意到,實作 call
選項的基礎是 Map
,因此在指令列上重複執行 --call
時,這些選項都會儲存。
timeout
選項具有 long
的基礎實作,只能儲存一個值。因此,系統只會儲存指定的最後一個值。--timeout 5 --timeout 10
結果在 timeout
中包含 10。
如果 List
或 Collection
是基礎實作項目,則會依指令列指定的順序儲存所有值。
布林值選項
布林值基礎類型的選項可以直接傳遞選項名稱 (例如 --[option-name]
) 來設為 true
,也可以使用 --no-[option-name]
語法設為 false
。
另請參閱
將選項傳遞至套件和模組
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Option handling in Tradefed\n\nOption handling lies at the heart of Trade Federation's modular approach. In particular, options\nare the mechanism by which the developer, Integrator, and Test Runner can work together without\nhaving to duplicate each-other's work. Put simply, our implementation of option handling allows the\ndeveloper to mark a Java class member as being configurable, at which point the value of that member\ncan be augmented or overridden by the Integrator, and can be subsequently augmented or overridden by\nthe Test Runner. This mechanism works for all Java intrinsic types, as well as for any\n`Map` or `Collection` instances of intrinsic types.\n\n**Note:** The option-handling mechanism only works for classes\nimplementing one of the\ninterfaces included in the [Test Lifecycle](/docs/core/tests/tradefed/fundamentals/lifecycle), and only when that class is\n*instantiated* by the lifecycle machinery.\n\nDeveloper\n---------\n\nTo start off, the developer marks a member with the\n[@Option](https://android.googlesource.com/platform/tools/tradefederation/+/android16-release/common_util/com/android/tradefed/config/Option.java) annotation.\nThey specify (at a minimum) the `name` and `description` values, which\nspecify the argument name associated with that Option, and the description that is displayed on\nthe TF console when the command is run with `--help` or `--help-all`.\n\nAs an example, let's say we want to build a functional phone test that dials a variety of\nphone numbers, and expects to receive a sequence of DTMF tones from each number after it\nconnects. \n\n```gdscript\npublic class PhoneCallFuncTest extends IRemoteTest {\n @Option(name = \"timeout\", description = \"How long to wait for connection, in millis\")\n private long mWaitTime = 30 * 1000; // 30 seconds\n\n @Option(name = \"call\", description = \"Key: Phone number to attempt. \" +\n \"Value: DTMF to expect. May be repeated.\")\n private Map\u003cString, String\u003e mCalls = new HashMap\u003cString, String\u003e;\n\n public PhoneCallFuncTest() {\n mCalls.add(\"123-456-7890\", \"01134\"); // default\n }\n```\n\nThat's all that's required for the developer to set up two points of configuration for that\ntest. They could then go off and use `mWaitTime` and `mCalls` as normal,\nwithout paying much attention to the fact that they're configurable. Because the\n`@Option` fields are set after the class is instantiated, but before the\n`run` method is called, that provides an easy way for implementors to set up defaults for\nor perform some kind of filtering on `Map` and `Collection` fields, which are\notherwise append-only.\n\nIntegrator\n----------\n\nThe Integrator works in the world of configurations, which are written in XML. The config format\nallows the Integrator to set (or append) a value for any `@Option` field. For instance,\nsuppose the Integrator wanted to define a lower-latency test that calls the default number, as well\nas a long-running test that calls a variety of numbers. They could create a pair of configurations\nthat might look like the following: \n\n```world-of-warcraft-toc\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cconfiguration description=\"low-latency default test; low-latency.xml\"\u003e\n \u003ctest class=\"com.example.PhoneCallFuncTest\"\u003e\n \u003coption name=\"timeout\" value=\"5000\" /\u003e\n \u003c/test\u003e\n\u003c/configuration\u003e\n``` \n\n```world-of-warcraft-toc\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cconfiguration description=\"call a bunch of numbers; many-numbers.xml\"\u003e\n \u003ctest class=\"com.example.PhoneCallFuncTest\"\u003e\n \u003coption name=\"call\" key=\"111-111-1111\" value=\"#*#*TEST1*#*#\" /\u003e\n \u003coption name=\"call\" key=\"222-222-2222\" value=\"#*#*TEST2*#*#\" /\u003e\n \u003c!-- ... --\u003e\n \u003c/test\u003e\n\u003c/configuration\u003e\n```\n\nTest Runner\n-----------\n\nThe Test Runner also has access to these configuration points through the Trade Federation console.\nFirst and foremost, they run a command (that is, a config and all of its arguments) with the\n`run command \u003cname\u003e` instruction (or `run \u003cname\u003e` for short).\nBeyond that, they can specify any list of arguments are part of the command, which can replace or\nappend to fields specified by lifecycle objects within each config.\n\nTo run the low-latency test with the `many-numbers` phone numbers, the Test Runner\ncould execute: \n\n```\ntf\u003e run low-latency.xml --call 111-111-1111 #*#*TEST1*#*# --call 222-222-2222 #*#*TEST2*#*#\n```\n\nOr, to get a similar effect from the opposite direction, the Test Runner could reduce the wait\ntime for the `many-numbers` test: \n\n```\ntf\u003e run many-numbers.xml --timeout 5000\n```\n\nOption ordering\n---------------\n\nYou might notice that the `call` option underlying implementation is a `Map`\nso upon repeated `--call` on the command line, they're all stored.\n\nThe option `timeout`, which has an underlying implementation of `long`,\ncan only store one value. So only the last value specified is stored.\n`--timeout 5 --timeout 10` results in `timeout` containing 10.\n\nIn case of a `List` or `Collection` as the underlying implementation,\nall the values are stored, in the order specified on the command line.\n\nBoolean options\n---------------\n\nOptions of boolean underlying type can be set to `true` by directly passing the\noption name, for example, `--[option-name]` and can be set to `false` using\nthe syntax `--no-[option-name]`.\n\nSee also\n--------\n\n[Pass options to suite and\nmodules](/docs/core/tests/tradefed/testing/through-suite/option-passing)"]]