自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
目标准备器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
目标准备器在定义它们的测试级别中的测试之前调用。这样,您就可以设置任何设备,以使测试顺畅地运行。
基础接口
基接口是 ITargetPreparer
,用于实现要执行的 setUp
方法。建议您实现我们的基本抽象类 BaseTargetPreparer
,它提供了内置的停用功能来轻松停用准备器。
清理器接口
setUp
的自然扩展是 tearDown
,由其他接口 ITargetCleaner
提供。这提供了 tearDown
接口,可用于清理测试执行后 setUp
中完成的任何任务。
BaseTargetPreparer
类也会扩展 ITargetCleaner
。
建议
我们建议每个准备器限定于一项主要功能,例如安装 APK 或运行命令。这样,就可以更容易地重复使用准备器。
此外,请在添加新的准备器之前检查可用准备器的列表,以避免重复工作。准备器位于 tools/tradefederation/core/src/com/android/tradefed/targetprep/
。
XML 配置
对象标记为 target_preparer
,例如:
<target_preparer class="com.android.tradefed.targetprep.InstallApkSetup">
<option name="install-arg" value="-d"/>
</target_preparer>
如需了解相关背景信息,另请参阅设置套件。
顶级设置
如果是在顶级设置中指定的准备器,则准备器会仅针对每个设备运行一次。例如 cts-common.xml
,它是 Android 兼容性测试套件 (CTS) 测试的顶级设置。
模块级设置
如果是在模块级设置中指定的准备器,则准备器将始终在该模块之前运行。例如 backup/AndroidTest.xml
,它定义 Tradefed 如何运行 backup
CTS 模块。
请注意,虽然准备器将在相应模块之前运行,但会在所有系统状态检查工具之后运行。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Target preparers are invoked before the tests in the\n[test level](/docs/core/tests/tradefed/testing/through-suite/setup#definitions)\nin which they are defined. This allows the setup of any device for tests to run\nsmoothly.\n\nBase interface\n--------------\n\nThe base interface is\n[`ITargetPreparer`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/ITargetPreparer.java),\nwhich allows implementation of a `setUp` method to be executed. We\nrecommend implementing our basic abstract class\n[`BaseTargetPreparer`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/BaseTargetPreparer.java),\nwhich provides a built-in disablement feature to easily disable a preparer.\n\nCleaner interface\n-----------------\n\nThe natural extension of `setUp` is `tearDown` and is provided by a different\ninterface,\n[`ITargetCleaner`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/ITargetCleaner.java). That provides the `tearDown` interface\nthat allows cleaning up anything that was done in `setUp` after the test\nexecution.\n\nThe `BaseTargetPreparer` class also extends `ITargetCleaner`.\n\nRecommendations\n---------------\n\nWe recommend each preparer be limited to a single main function, for example\ninstalling an APK or running a command. This allows for easier reuse of\npreparers.\n\nAlso check the list of available preparers before adding a new one to\navoid duplicating work. Preparers are available in [`tools/tradefederation/core/src/com/android/tradefed/targetprep/`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/src/com/android/tradefed/targetprep/).\n\nXML configuration\n-----------------\n\nThe object tag is `target_preparer`, for example: \n\n \u003ctarget_preparer class=\"com.android.tradefed.targetprep.InstallApkSetup\"\u003e\n \u003coption name=\"install-arg\" value=\"-d\"/\u003e\n \u003c/target_preparer\u003e\n\nAlso refer to\n[Set up suites](/docs/core/tests/tradefed/testing/through-suite/setup)\nfor context.\n\n### Top-level setup\n\nIf specified in a top-level setup, the preparer runs only once for each\ndevice. An example is\n[`cts-common.xml`](https://android.googlesource.com/platform/cts/+/refs/heads/android16-release/tools/cts-tradefed/res/config/cts-common.xml),\nwhich is a top-level setup for Android Compatibility Test Suite (CTS) tests.\n\n### Module-level setup\n\nIf specified at the module level, the preparer always runs before that\nmodule. An example is\n[`backup/AndroidTest.xml`](https://android.googlesource.com/platform/cts/+/refs/heads/android16-release/tests/backup/AndroidTest.xml),\nwhich defines how Tradefed runs the `backup` CTS module.\n\nNote that while the preparer runs before the module, it runs *after* any\n[system status checkers](/docs/core/tests/tradefed/testing/through-suite/system-status-checker)."]]