Espresso
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Sử dụng Espresso để viết quy trình kiểm thử giao diện người dùng Android ngắn gọn, đẹp mắt và đáng tin cậy.
Đoạn mã sau đây cho thấy một ví dụ về quy trình kiểm thử 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 cốt lõi có kích thước nhỏ, có thể dự đoán, dễ học và vẫn mở cho
phần tuỳ chỉnh. Espresso kiểm thử trạng thái kỳ vọng, tương tác và câu nhận định
rõ ràng mà không làm sao lãng nội dung nguyên mẫu, cơ sở hạ tầng tuỳ chỉnh,
hoặc các chi tiết triển khai lộn xộn gây cản trở cho bạn.
Quy trình kiểm thử Espresso chạy nhanh tối ưu! Tính năng này cho phép bạn chờ, đồng bộ hoá, ngủ
và cuộc thăm dò ý kiến phía sau đồng thời
thao tác và xác nhận trên ứng dụng
Giao diện người dùng khi thiết bị ở trạng thái tĩnh.
Đối tượng mục tiêu
Espresso nhắm đến các nhà phát triển. Họ tin rằng kiểm thử tự động là một cách
phần không thể thiếu của vòng đời phát triển. Mặc dù nó có thể dùng cho hộp đen
thử nghiệm, toàn bộ sức mạnh của Espresso được khai thác bởi những ai đã quen thuộc với
cơ sở mã đang được kiểm thử.
Khả năng đồng bộ hoá
Mỗi khi thử nghiệm gọi
onView()
!
Espresso chờ thực hiện thao tác hoặc câu nhận định tương ứng trên giao diện người dùng cho đến khi
các điều kiện đồng bộ hoá sau được đáp ứng:
- Hàng đợi thông báo không có thông báo nào mà Espresso cần ngay lập tức
của chúng tôi.
- Hiện không có bản sao
AsyncTask
nào đang thực thi
một việc cần làm.
- Tất cả do nhà phát triển xác định
idling resource (tài nguyên không hoạt động) đang ở trạng thái rảnh.
Bằng cách thực hiện các bước kiểm tra này, Espresso tăng đáng kể khả năng
chỉ có thể xảy ra một hành động hoặc câu nhận định trên giao diện người dùng vào một thời điểm bất kỳ. Chức năng này
cho bạn kết quả kiểm thử đáng tin cậy hơn.
Gói
espresso-core
– Chứa các trình so khớp View
cốt lõi và cơ bản, các hành động, và
xác nhận. Xem
Kiến thức cơ bản
và Công thức.
espresso-web
– Chứa các tài nguyên hỗ trợ về WebView
.
espresso-idling-resource
–
Cơ chế đồng bộ hoá với các công việc ở chế độ nền của Espresso.
espresso-contrib
- Đóng góp bên ngoài chứa DatePicker
,
Các thao tác RecyclerView
và Drawer
, kiểm tra khả năng hỗ trợ tiếp cận và
CountingIdlingResource
.
espresso-intents
–
Phần mở rộng để xác thực và giả lập các ý định cho kiểm thử khép kín.
espresso-remote
– Vị trí của chức năng đa quy trình của Espresso.
Bạn có thể tìm hiểu thêm về những phiên bản mới nhất bằng cách đọc
ghi chú phát hành.
Tài nguyên khác
Để biết thêm thông tin về cách sử dụng Espresso trong quy trình kiểm thử Android, hãy tham khảo
các tài nguyên sau đây.
Mẫu
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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)"]]