Espresso
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Espresso 編寫簡潔、美觀且可靠的 Android UI 測試。
以下程式碼片段為 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 測試的執行速度非常快!讓你在等待、同步、睡眠和睡眠
對應用程式進行操作及斷言時,系統會對背景資料進行輪詢
也就是 UI
目標對象
Espresso 是專為開發人員打造,他們認為自動化測試是指
是開發生命週期中不可或缺的一環雖然可用於黑箱作業
測試時,如果能夠熟悉 Espresso 的
測試中的程式碼集
同步處理功能
每次測試叫用時
onView()
、
Espresso 會等待執行對應的 UI 操作或斷言,直到
符合下列條件:
- 訊息佇列沒有任何 Espresso 必須立即顯示的訊息
上傳資料集之後,您可以運用 AutoML
自動完成部分資料準備工作
- 目前沒有執行中的「
AsyncTask
」執行個體
或稱做工作
- 所有由開發人員定義
閒置資源處於閒置狀態。
執行這些檢查後,Espresso 大幅提高
一次只能執行一個 UI 操作或斷言。這項功能
,取得更可靠且可靠的測試結果。
套件
espresso-core
- 包含核心和基本 View
比對器、動作和
斷言。詳情請見
基本資訊
和食譜。
espresso-web
:包含 WebView
支援的資源。
espresso-idling-resource
:
Espresso 的背景工作同步處理機制。
espresso-contrib
- 含有 DatePicker
的外部貢獻內容,
RecyclerView
和 Drawer
動作、無障礙功能檢查和
CountingIdlingResource
。
espresso-intents
:
用於驗證和虛設常式測試用的擴充功能。
espresso-remote
- Espresso 多程序功能的位置。
如要進一步瞭解最新版本,請參閱
版本資訊。
其他資源
如要進一步瞭解如何在 Android 測試中使用 Espresso,請參閱
資源。
範例
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# 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)"]]