自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
Trade Federation 概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Trade Federation(简称 Tradefed 或 TF)是一种连续的测试框架,专门用于在 Android 设备上运行测试。例如,Tradefed 用于运行兼容性测试套件 (CTS) 和供应商测试套件 (VTS)。
Trade Federation 是一款在主机上运行的 Java 应用,它会使用 ddmlib(DDMS 背后的内容库)通过 adb 与一部或多部 Android 设备进行通信。
我们在下面列出了 TF 的一些主要功能以及几个用例示例。也就是说,如果您想直接进入正题并开始使用,则可直接前往开始使用页面。
功能
- 模块化、灵活性、可伸缩设计
- 已内置对运行很多不同类型 Android 测试的支持:instrumentation、uiautomator、原生测试/gtest、基于主机的 JUnit 测试等等
- 在 adb 的基础之上提供可靠性和恢复机制
- 支持同时在多部设备上调度和运行测试
有关如何运行现有测试(例如插桩测试)的最新信息,请参阅通过 TF 进行测试。
用例
Trade Federation 的模块化设计使得融入已有构建、测试和报告基础架构的环境非常简单。我们在下面列出了几个示范性用例,在这些用例中,tradefed 能够实施有效的可扩展测试做法。
首先,从“哪些部分可被修改,哪些部分固定不变?”这个问题来考虑潜在用例的格局非常有用。例如,原始设备制造商 (OEM) 可以修改框架、系统和硬件,但对现有应用的影响微乎其微。与此相反,应用开发者可以修改应用,但对系统或框架的大多数方面几乎没有控制权。
因此,每个用例中的实体将会具有不同的测试目标,并会具有不同的选项(如果一组测试失败的话)。尽管存在这些差异,Trade Federation 仍有助于确保每个测试流程有效、灵活且可扩展。
原始设备制造商
原始设备制造商会构建硬件,且通常会调整 Android 系统和框架以使其在该硬件上运行良好。OEM 可能会努力实现这些目标,同时在硬件和系统级别保持稳定性和性能,并确保框架变更不会破坏与现有应用的兼容性。
OEM 可以实现将在生命周期的目标设置阶段执行的设备刷写模块。该模块将在其执行期间完全控制设备,所以它可能会强制设备进入引导加载程序、进行刷写,然后强制设备重新启动回用户空间模式。与模块结合以连接到连续构建系统,这使得 OEM 能够在更改系统级固件和 Java 级框架时在其设备上运行测试。
当设备完全启动后,OEM 商将能够利用基于 JUnit 的现有测试,或者编写新测试,来验证相关功能。最后,他们可以编写一个或多个结果报告模块,以连接到现有的测试结果存储区,或者直接报告结果(例如通过电子邮件)。
应用开发者
应用开发者负责构建应用,他们构建的应用需要能够在各种平台版本和各种设备上运行良好。如果某个特定的平台版本和/或设备出现问题,唯一的补救措施就是添加解决方法并继续操作。对于大型开发者而言,测试流程可能会并入连续构建序列。对于小型开发团队而言,测试流程可能会定期或手动启动。
大多数应用开发者会使用 TF 中已有的 APK 测试安装模块。有一个版本可从本地文件系统安装,还有一个版本可安装从 build 服务下载的 APK。请务必注意,无论同一宿主机上运行着多少个 TF 实例,后一版本都能持续正常运行。
由于 TF 能够游刃有余地处理多部设备,所以按照该测试所用设备的类型对每项测试结果进行分类会很简便。因此,TF 有可能会分别为相应应用的每个版本生成一个二维(或多维)兼容性矩阵。
测试服务
举例来说,某项测试服务可能让应用开发者既能提交应用,又能在使用功耗测量工具来确定应用耗电量的设备上运行测试。这与之前的两个用例不同,因为服务构建器并不控制设备或正在运行的应用。
由于 Trade Federation 可运行任何能实现简单 IRemoteTest
接口的 Java 类,因此可以轻松编写可协调某些外部硬件与在设备上运行的测试用例的驱动程序。驱动程序本身可以生成线程、向其他服务器发送请求,或执行它可能需要的其他任何操作。此外,结果报告接口 ITestInvocationListener
的简单性和多功能性意味着,表示任意测试结果(例如,数字功率指标)传递到标准结果报告管道中。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Trade Federation overview\n\nTrade Federation (Tradefed or TF for short) is a continuous test framework designed for running\ntests on Android devices. For example, Tradefed is used to run the\n[Compatibility Test Suite (CTS)](/docs/compatibility/cts) and\n[Vendor Test Suite (VTS)](/docs/core/tests/vts).\n\nTrade Federation is a Java application which runs on a host computer, and communicates to one or\nmore Android devices using ddmlib (the library behind DDMS) over adb.\n\nWe've listed some of TF's main features below, along with a couple sample usecases. That said,\nif you want to jump right in and get started, you can head straight for the\n[Start Here](/docs/core/tests/tradefed/fundamentals) page.\n\nFeatures\n--------\n\n- modular, flexible, scalable design\n- has built in support for running many different types of Android tests: [instrumentation](https://developer.android.com/studio/test#create_instrumented_test_for_a_build_variant), [uiautomator](https://developer.android.com/training/testing/ui-testing), native/gtest, host-based JUnit, etc\n- provides reliability and recovery mechanisms on top of adb\n- supports scheduling and running tests on multiple devices in parallel\n\nSee [Testing Through TF](/docs/core/tests/tradefed/testing/through-tf)\nfor the most up-to-date information about how to run your existing tests, such as\n[Instrumentation](/docs/core/tests/tradefed/testing/through-tf/instrumentation).\n\nUse cases\n---------\n\nTrade Federation's modularity makes it straightforward to slot into environments with existing\nbuild, test, and reporting infrastructures. We list below a few demonstrative\nusecases where tradefed could enable efficient, scalable test practices.\n\nFirst, it is useful to consider the landscape of potential usecases in terms of the question\n\"which parts are modifiable, and what parts are static?\" For instance, a Device OEM can modify the\nframework, the system, and the hardware, but has little or no influence over existing applications.\nAn application developer, on the other hand, can modify the app, but has little control over most\naspects of the system or the framework.\n\nAs a result, an entity in each usecase will have different testing goals, and will have different\noptions in the case of a set of test failures. Despite these differences, Trade Federation can\nhelp make each of their test processes efficient, flexible, and scalable.\n\n### Device OEM\n\nA Device OEM builds hardware, and will often tweak the Android system and frameworks to run well\non that hardware. The OEM might strive to accomplish those goals while retaining stability\nand performance at the hardware and system levels, and making sure the framework changes don't break\ncompatibility with existing applications.\n\nThe OEM could implement a device flashing module that will execute during the Target Setup stage\nof the [lifecycle](/docs/core/tests/tradefed/fundamentals/lifecycle). That\nmodule would have complete control over the device during its execution period, which would allow\nit to potentially force the device into the bootloader, flash, and then force the device to reboot\nback into userspace mode. Combined with a module to tie into a continuous build system, this would\nallow the OEM to run tests on their device as they make changes to the system-level firmware and\nJava-level frameworks.\n\nOnce the device is fully booted, the OEM would be able to leverage existing JUnit-based tests,\nor write new ones, to verify the functionality of interest. Finally, they could write one or more\nresult reporting modules to tie into existing test-result repositories, or to report results\ndirectly (for instance,\n[by email](/reference/com/android/tradefed/result/EmailResultReporter)).\n\n### App developer\n\nAn Application Developer builds an app which needs to run well across a variety of platform\nversions and a variety of devices. If an issue comes up on a particular platform version and/or\ndevice, the only remedy is to add a workaround and move on. For larger developers, the test\nprocess might be incorporated into a continuous build sequence. For smaller developers, it\nmight be kicked off periodically or by hand.\n\nMost app developers would use the apk test installation modules that already exist in TF.\nThere's a version that [installs from the local filesystem](/reference/com/android/tradefed/targetprep/InstallApkSetup), as well as a version that can\n[install\napks downloaded from a build service](/reference/com/android/tradefed/targetprep/InstallBuildEnvApkSetup). It is important to note that the latter version would\ncontinue to work properly with arbitrarily many TF instances running on the same host machine.\n\nBecause of TF's proficiency at dealing with multiple devices, it would be straightforward to\nclassify each test result by the type of device that was used for that test. Thus, TF could\npotentially generate a 2-dimensional (or multi-dimensional) compatibility matrix for every\nbuild of the application.\n\n### Testing service\n\nA Test Service might, for instance, allow app developers to submit apps and run tests on devices\ninstrumented with power-measurement tools to determine power usage for the app. This differs from\nthe prior two usecases in that the service builder does not control the devices or the applications\nthat are being run.\n\nBecause Trade Federation can run any Java class that implements the simple\n[`IRemoteTest`](/reference/com/android/tradefed/testtype/IRemoteTest) interface, it's\ntrivial to write drivers that can coordinate some external piece of hardware with the test case\nthat's being run on the device. The driver itself can spawn Threads, send requests to other\nservers, or do anything else that it might need. Moreover, the simplicity and versatility of the\nresult reporting interface,\n[`ITestInvocationListener`](/reference/com/android/tradefed/result/ITestInvocationListener), means that it is likewise straightforward to\nrepresent arbitrary test results (including, for instance, numerical power metrics) into the\nstandard result reporting pipeline."]]