Kiểm thử bố cục Compose
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.
Kiểm thử giao diện người dùng của ứng dụng để xác minh rằng hành vi của mã Compose là chính xác. Nhờ đó, bạn có thể phát hiện lỗi sớm và cải thiện chất lượng ứng dụng.
Compose cung cấp một bộ API kiểm thử để tìm các phần tử, xác minh thuộc tính của phần tử đó và thực hiện các hành động của người dùng. Các API này cũng bao gồm các tính năng nâng cao chẳng hạn như thao tác thời gian. Hãy dùng các API này để tạo các kiểm thử mạnh mẽ nhằm xác minh hành vi của ứng dụng.
Số lượt xem
Nếu bạn đang làm việc với các khung hiển thị thay vì Compose, hãy xem phần Kiểm thử ứng dụng trên Android.
Cụ thể, bạn nên bắt đầu bằng hướng dẫn Tự động hoá quy trình kiểm thử giao diện người dùng. Nội dung này trình bày cách bạn có thể tự động hoá các kiểm thử chạy trên thiết bị, kể cả khi sử dụng các khung hiển thị.
Khái niệm chính
Sau đây là một số khái niệm chính để kiểm thử mã Compose:
- Ngữ nghĩa: Ngữ nghĩa cung cấp ý nghĩa cho giao diện người dùng của bạn, cho phép các quy trình kiểm thử tương tác với các phần tử cụ thể.
- API kiểm thử: API kiểm thử cho phép bạn tìm các phần tử, xác minh thuộc tính của các phần tử đó và thực hiện các hành động của người dùng.
- Đồng bộ hoá: Đồng bộ hoá giúp xác minh rằng các quy trình kiểm thử chờ giao diện người dùng ở trạng thái rảnh trước khi thực hiện các hành động hoặc đưa ra câu nhận định.
- Khả năng tương tác: Khả năng tương tác cho phép các quy trình kiểm thử hoạt động với cả các phần tử dựa trên Compose và Khung hiển thị trong cùng một ứng dụng.
Bản tóm tắt về kiểm thử
Hãy xem bản tóm tắt về kiểm thử để biết thông tin tổng quan về tất cả các chủ đề chính mà bạn nên tìm hiểu về hoạt động kiểm thử trong Compose.
Thiết lập
Thiết lập ứng dụng để bạn có thể kiểm thử mã Compose.
Trước tiên, hãy thêm các phần phụ thuộc sau vào tệp build.gradle
của mô-đun
chứa các quy trình kiểm thử giao diện người dùng của bạn:
// Test rules and transitive dependencies:
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
// Needed for createComposeRule(), but not for createAndroidComposeRule<YourActivity>():
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
Mô-đun này bao gồm ComposeTestRule
và một phương thức triển khai cho Android có tên là AndroidComposeTestRule
. Thông qua quy tắc này, bạn có thể thiết lập nội dung Compose hoặc truy cập hoạt động. Bạn tạo các quy tắc bằng cách sử dụng các hàm của nhà máy, có thể là createComposeRule
hoặc createAndroidComposeRule
nếu bạn cần truy cập vào một hoạt động. Quy trình kiểm thử giao diện người dùng điển hình của Compose có dạng như sau:
// file: app/src/androidTest/java/com/package/MyComposeTest.kt
class MyComposeTest {
@get:Rule val composeTestRule = createComposeRule()
// use createAndroidComposeRule<YourActivity>() if you need access to
// an activity
@Test
fun myTest() {
// Start the app
composeTestRule.setContent {
MyAppTheme {
MainScreen(uiState = fakeUiState, /*...*/)
}
}
composeTestRule.onNodeWithText("Continue").performClick()
composeTestRule.onNodeWithText("Welcome").assertIsDisplayed()
}
}
Tài nguyên khác
- Kiểm thử ứng dụng trên Android: Trang đích chính về kiểm thử trên Android
cung cấp thông tin tổng quan về các nguyên tắc cơ bản và kỹ thuật kiểm thử.
- Nguyên tắc cơ bản về kiểm thử: Tìm hiểu thêm
về các khái niệm cốt lõi đằng sau việc kiểm thử một ứng dụng Android.
- Kiểm thử cục bộ: Bạn có thể chạy một số bài kiểm thử
cục bộ trên máy trạm của riêng mình.
- Kiểm thử đo lường: Bạn cũng nên
chạy kiểm thử đo lường. Tức là các kiểm thử chạy trực tiếp
trên thiết bị.
- Tích hợp liên tục:
Tích hợp liên tục cho phép bạn tích hợp các kiểm thử vào quy trình triển khai.
- Kiểm thử nhiều kích thước màn hình: Vì người dùng có thể sử dụng nhiều thiết bị, nên bạn cần kiểm thử đối với nhiều kích thước màn hình.
- Espresso: Mặc dù được thiết kế cho giao diện người dùng dựa trên Khung hiển thị, nhưng kiến thức về Espresso vẫn có thể hữu ích cho một số khía cạnh của hoạt động kiểm thử trong Compose.
Lớp học lập trình
Để tìm hiểu thêm, hãy thử tham gia Lớp học lập trình Kiểm thử trong Jetpack Compose.
Mẫu
Đề xuất cho bạn
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-08-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-08-27 UTC."],[],[],null,["Test your app's UI to verify that behavior of your Compose code is\ncorrect. This lets you catch errors early and improve the quality of your app.\n\nCompose provides a set of testing APIs to find elements, verify their\nattributes, and perform user actions. The APIs also include advanced features\nsuch as time manipulation. Use these APIs to create robust tests that verify\nyour app's behavior.\n| **Important:** For more on testing Android apps in general, including testing `View` elements, see the [general testing section](/training/testing). A good place to start is the [Fundamentals of testing Android apps](/training/testing/fundamentals) guide.\n\nViews\n\nIf you are working with views instead of Compose, see the general [Test apps on\nAndroid](/training/testing) section.\n\nIn particular, a good place to start is the [Automate UI tests](/training/testing/ui-tests) guide. It\nlays out how you can automate tests that run on-device, including when using\nviews.\n\nKey Concepts\n\nThe following are some key concepts for testing your Compose code:\n\n- **[Semantics](/develop/ui/compose/testing/semantics)**: Semantics give meaning to your UI, allowing tests to interact with specific elements.\n- **[Testing APIs](/develop/ui/compose/testing/apis)**: Testing APIs let you find elements, verify their attributes, and perform user actions.\n- **[Synchronization](/develop/ui/compose/testing/synchronization)**: Synchronization verifies that tests wait for the UI to be idle before performing actions or making assertions.\n- **[Interoperability](/develop/ui/compose/testing/interoperability)**: Interoperability enables tests to work with both Compose and View-based elements in the same app.\n\nTesting cheatsheet\n\nSee the [testing cheatsheet](/develop/ui/compose/testing/testing-cheatsheet) for an overview of all the key topics you should\nlearn about testing in Compose.\n\nSetup\n\nSet up your app to let you test compose code.\n\nFirst, add the following dependencies to the `build.gradle` file of the module\ncontaining your UI tests: \n\n // Test rules and transitive dependencies:\n androidTestImplementation(\"androidx.compose.ui:ui-test-junit4:$compose_version\")\n // Needed for createComposeRule(), but not for createAndroidComposeRule\u003cYourActivity\u003e():\n debugImplementation(\"androidx.compose.ui:ui-test-manifest:$compose_version\")\n\nThis module includes a [`ComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule) and an implementation for Android\ncalled [`AndroidComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/AndroidComposeTestRule). Through this rule you can set Compose\ncontent or access the activity. You construct the rules using factory functions,\neither [`createComposeRule`](/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createComposeRule()) or, if you need access to an activity,\n[`createAndroidComposeRule`](/reference/kotlin/androidx/compose/ui/test/junit4/package-summary#createAndroidComposeRule()). A typical UI test for Compose looks like this: \n\n // file: app/src/androidTest/java/com/package/MyComposeTest.kt\n\n class MyComposeTest {\n\n @get:Rule val composeTestRule = createComposeRule()\n // use createAndroidComposeRule\u003cYourActivity\u003e() if you need access to\n // an activity\n\n @Test\n fun myTest() {\n // Start the app\n composeTestRule.setContent {\n MyAppTheme {\n MainScreen(uiState = fakeUiState, /*...*/)\n }\n }\n\n composeTestRule.onNodeWithText(\"Continue\").performClick()\n\n composeTestRule.onNodeWithText(\"Welcome\").assertIsDisplayed()\n }\n }\n\nAdditional Resources\n\n- **[Test apps on Android](/training/testing)**: The main Android testing landing page provides a broader view of testing fundamentals and techniques.\n- **[Fundamentals of testing](/training/testing/fundamentals):** Learn more about the core concepts behind testing an Android app.\n- **[Local tests](/training/testing/local-tests):** You can run some tests locally, on your own workstation.\n- **[Instrumented tests](/training/testing/instrumented-tests):** It is good practice to also run instrumented tests. That is, tests that run directly on-device.\n- **[Continuous integration](/training/testing/continuous-integration):** Continuous integration lets you integrate your tests into your deployment pipeline.\n- **[Test different screen sizes](/training/testing/different-screens):** With some many devices available to users, you should test for different screen sizes.\n- **[Espresso](/training/testing/espresso)**: While intended for View-based UIs, Espresso knowledge can still be helpful for some aspects of Compose testing.\n\nCodelab\n\nTo learn more, try the [Jetpack Compose Testing codelab](/codelabs/jetpack-compose-testing).\n\nSamples\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Semantics in Compose](/develop/ui/compose/semantics)\n- [Window insets in Compose](/develop/ui/compose/layouts/insets)\n- [Other considerations](/develop/ui/compose/migrate/other-considerations)"]]