Espresso
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 Espresso 来编写简洁、美观且可靠的 Android 界面测试。
以下代码段展示了 Espresso 测试的一个示例:
Kotlin
@Test
fun greeterSaysHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"))
onView(withId(R.id.greet_button)).perform(click())
onView(withText("Hello Steve!")).check(matches(isDisplayed()))
}
Java
@Test
public void greeterSaysHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"));
onView(withId(R.id.greet_button)).perform(click());
onView(withText("Hello Steve!")).check(matches(isDisplayed()));
}

核心 API 小巧、可预测、易于学习,并且对
自定义。Espresso 测试会说明预期、交互和断言
而不受样板内容、自定义基础架构、
或杂乱的实施细节造成障碍。
Espresso 测试运行速度极快!它让您远离等待、同步、睡眠、
并在应用上操作和断言时
静态数据。
目标受众群体
Espresso 面向那些认为自动化测试
开发生命周期不可或缺的一部分。虽然它可用于黑盒
熟悉测试工具的人员可以解锁 Espresso 的全部能力
受测代码库
同步功能
每次测试调用
onView()
、
Espresso 会等待执行相应的界面操作或断言,
满足以下同步条件:
- 消息队列中没有 Espresso 立即需要的任何消息
过程。
- 没有当前正在执行的“
AsyncTask
”实例
任务。
- 开发者定义的所有
空闲资源处于空闲状态。
通过执行这些检查,Espresso 可显著提高
在任何给定时间都只能发生一项界面操作或断言。此功能
为您提供更可靠、更可靠的测试结果。
软件包
espresso-core
- 包含核心和基本的 View
匹配器、操作和
断言。请参阅
基本信息
和食谱。
espresso-web
- 包含 WebView
支持的资源。
espresso-idling-resource
-
Espresso 与后台作业同步的机制。
espresso-contrib
- 包含 DatePicker
的外部贡献,
RecyclerView
和 Drawer
操作、无障碍功能检查以及
CountingIdlingResource
。
espresso-intents
-
用于对封闭测试的 intent 进行验证和打桩的扩展。
espresso-remote
- Espresso 的多进程功能的位置。
如需详细了解最新版本,请参阅
版本说明。
其他资源
如需详细了解如何在 Android 测试中使用 Espresso,请查阅
以下资源。
示例
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Espresso\n\nUse Espresso to write concise, beautiful, and reliable Android UI tests.\n\nThe following code snippet shows an example of an Espresso test:\n\n\u003cbr /\u003e\n\n### Kotlin\n\n```kotlin\n@Test\nfun greeterSaysHello() {\n onView(withId(R.id.name_field)).perform(typeText(\"Steve\"))\n onView(withId(R.id.greet_button)).perform(click())\n onView(withText(\"Hello Steve!\")).check(matches(isDisplayed()))\n}\n```\n\n### Java\n\n```java\n@Test\npublic void greeterSaysHello() {\n onView(withId(R.id.name_field)).perform(typeText(\"Steve\"));\n onView(withId(R.id.greet_button)).perform(click());\n onView(withText(\"Hello Steve!\")).check(matches(isDisplayed()));\n}\n```\n\n\u003cbr /\u003e\n\nThe core API is small, predictable, and easy to learn and yet remains open for\ncustomization. Espresso tests state expectations, interactions, and assertions\nclearly without the distraction of boilerplate content, custom infrastructure,\nor messy implementation details getting in the way.\n\nEspresso tests run optimally fast! It lets you leave your waits, syncs, sleeps,\nand polls behind while it manipulates and asserts on the application\nUI when it is at rest.\n\nTarget audience\n---------------\n\nEspresso is targeted at developers, who believe that automated testing is an\nintegral part of the development lifecycle. While it can be used for black-box\ntesting, Espresso's full power is unlocked by those who are familiar with the\ncodebase under test.\n\nSynchronization capabilities\n----------------------------\n\nEach time your test invokes\n[`onView()`](/reference/androidx/test/espresso/Espresso#onView(org.hamcrest.Matcher%3Candroid.view.View%3E)),\nEspresso waits to perform the corresponding UI action or assertion until the\nfollowing synchronization conditions are met:\n\n- The message queue doesn't have any messages that Espresso needs to immediately process.\n- There are no instances of [AsyncTask](/reference/android/os/AsyncTask) currently executing a task.\n- All developer-defined [idling resources](/training/testing/espresso/idling-resource) are idle.\n\nBy performing these checks, Espresso substantially increases the likelihood that\nonly one UI action or assertion can occur at any given time. This capability\ngives you more reliable and dependable test results.\n\nPackages\n--------\n\n- `espresso-core` - Contains core and basic `View` matchers, actions, and assertions. See [Basics](/training/testing/espresso/basics) and [Recipes](/training/testing/espresso/recipes).\n- [`espresso-web`](/training/testing/espresso/web) - Contains resources for `WebView` support.\n- [`espresso-idling-resource`](/training/testing/espresso/idling-resource) - Espresso's mechanism for synchronization with background jobs.\n- `espresso-contrib` - External contributions that contain `DatePicker`, `RecyclerView` and `Drawer` actions, accessibility checks, and `CountingIdlingResource`.\n- [`espresso-intents`](/training/testing/espresso/intents) - Extension to validate and stub intents for hermetic testing.\n- `espresso-remote` - Location of Espresso's [multi-process](/training/testing/espresso/multiprocess) functionality.\n\nYou can learn more about the latest versions by reading the\n[release notes](/topic/libraries/testing-support-library/release-notes).\n\nAdditional resources\n--------------------\n\nFor more information about using Espresso in Android tests, consult the\nfollowing resources.\n\n### Samples\n\n- [Espresso Code Samples](https://github.com/googlesamples/android-testing) includes a full selection of Espresso samples.\n- [BasicSample](https://github.com/android/testing-samples/tree/main/ui/espresso/BasicSample): Basic Espresso sample.\n- [(more...)](/training/testing/espresso/additional-resources#samples)"]]