行为界面测试
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
行为界面测试是一种测试,用于分析界面层次结构,以便对界面元素的属性做出断言。
Jetpack 框架
Jetpack 包含各种框架,这些框架提供了用于编写界面测试的 API:
- Espresso 测试框架(Android 4.0.1、API 级别 14 或更高版本)提供了一些 API,用于编写界面测试以模拟单个目标应用内用户与 View 的交互。使用 Espresso 的主要好处是,它可以自动同步测试操作与您正在测试的应用的界面。Espresso 会检测主线程何时处于空闲状态,以便可以在适当的时间运行测试命令,从而提高测试的可靠性。
- Jetpack Compose(Android 5.0、API 级别 21 或更高版本)提供了一组测试 API,用于启动 Compose 界面和组件并与之互动。与 Compose 元素的互动会与测试同步,并且可以完全控制时间、动画和重组。
- UI Automator(Android 4.3、API 级别 18 或更高版本)是一个界面测试框架,适用于整个系统上以及多个已安装应用间的跨应用功能界面测试。利用 UI Automator API,您可以执行在测试设备上打开“设置”菜单或应用启动器等操作。
- 借助 Robolectric(Android 4.1、API 级别 16 或更高版本),您可以创建在工作站或持续集成环境的常规 JVM(而非模拟器或设备)上运行的本地测试。它可以使用 Espresso 或 Compose 测试 API 与界面组件互动。
其他资源
如需详细了解如何创建界面测试,请参阅以下资源。
文档
Codelab
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Behavior UI Tests\n\nBehavior UI tests are tests that analyze the UI hierarchy to make assertions\non the properties of the UI elements.\n\nJetpack frameworks\n------------------\n\nJetpack includes various frameworks that provide APIs for writing UI tests:\n\n- The **[Espresso testing framework](/training/testing/espresso)** (Android 4.0.1, API level 14 or higher) provides APIs for writing UI tests to simulate user interactions with *Views* within a single target app. A key benefit of using Espresso is that it provides automatic synchronization of test actions with the UI of the app you are testing. Espresso detects when the main thread is idle, so it is able to run your test commands at the appropriate time, improving the reliability of your tests.\n- **[Jetpack Compose](/jetpack/compose)** (Android 5.0, API level 21 or higher) provides a set of [*testing APIs*](/jetpack/compose/testing) to launch and interact with Compose screens and components. Interactions with Compose elements are synchronized with tests and have complete control over time, animations and recompositions.\n- **[UI Automator](/training/testing/ui-automator)** (Android 4.3, API level 18 or higher) is a UI testing framework suitable for cross-app functional UI testing across system and installed apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher on a test device.\n- **[Robolectric](/training/testing/local-tests/robolectric)** (Android 4.1, API level 16 or higher) lets you create *local* tests that run on your workstation or continuous integration environment in a regular JVM, instead of on an emulator or device. It can use Espresso or Compose testing APIs to interact with UI components.\n\nAdditional resources\n--------------------\n\nFor more information about creating UI tests, consult the following resources.\n\n### Documentation\n\n- [Build instrumented tests](/training/testing/instrumented-tests)\n- [Espresso](/training/testing/espresso)\n- [Compose Testing](/jetpack/compose/testing)\n\n### Codelabs\n\n- [Introduction to Test Doubles and Dependency Injection](/codelabs/advanced-android-kotlin-training-testing-test-doubles)"]]