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.
Compose tích hợp với các khung kiểm thử phổ biến.
Khả năng tương tác với Espresso
Trong một ứng dụng kết hợp, bạn có thể tìm thấy các thành phần Compose bên trong các hệ phân cấp khung hiển thị; cũng như có thể tìm thấy các khung hiển thị bên trong các thành phần kết hợp Compose (thông qua thành phần kết hợp AndroidView).
Không cần thực hiện bước đặc biệt nào để so khớp một trong hai loại này. Bạn so khớp các thành phần hiển thị bằng onView của Espresso và so khớp các phần tử Compose bằng ComposeTestRule.
@TestfunandroidViewInteropTest(){// Check the initial state of a TextView that depends on a Compose state.Espresso.onView(withText("Hello Views")).check(matches(isDisplayed()))// Click on the Compose button that changes the state.composeTestRule.onNodeWithText("Click here").performClick()// Check the new value.Espresso.onView(withText("Hello Compose")).check(matches(isDisplayed()))}
Khả năng tương tác với UiAutomator
Theo mặc định, chỉ có thể truy cập thành phần kết hợp UiAutomator bằng các trình mô tả thuận tiện của chúng (văn bản hiển thị, nội dung mô tả, v.v.). Nếu muốn truy cập bất kỳ thành phần kết hợp nào bằng Modifier.testTag, bạn cần bật thuộc tính ngữ nghĩa testTagsAsResourceId cho cây con của thành phần kết hợp cụ thể. Việc kích hoạt hành vi này hữu ích đối với các thành phần kết hợp không có tên người dùng duy nhất nào khác, chẳng hạn như thành phần kết hợp có thể cuộn (ví dụ: LazyColumn).
Bạn chỉ có thể bật thuộc tính ngữ nghĩa một lần trong hệ phân cấp thành phần kết hợp để đảm bảo mọi thành phần kết hợp lồng với Modifier.testTag đều có thể truy cập được từ UiAutomator.
Scaffold(// Enables for all composables in the hierarchy.modifier=Modifier.semantics{testTagsAsResourceId=true}){// Modifier.testTag is accessible from UiAutomator for composables nested here.LazyColumn(modifier=Modifier.testTag("myLazyColumn")){// Content}}
Bạn có thể truy cập mọi thành phần kết hợp với Modifier.testTag(tag) bằng cách sử dụng By.res(resourceName) dùng tag giống như resourceName.
valdevice=UiDevice.getInstance(getInstrumentation())vallazyColumn:UiObject2=device.findObject(By.res("myLazyColumn"))// Some interaction with the lazyColumn.
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ử.
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.
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,["# Interoperability\n\nCompose integrates with common testing frameworks.\n\nInteroperability with Espresso\n------------------------------\n\nIn a hybrid app, you can find Compose components inside view hierarchies and\nviews inside Compose composables (via the [`AndroidView`](/reference/kotlin/androidx/compose/ui/viewinterop/package-summary#AndroidView(kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1)) composable).\n\nThere are no special steps needed to match either type. You match views with\nEspresso's [`onView`](/reference/androidx/test/espresso/Espresso#onView(org.hamcrest.Matcher%3Candroid.view.View%3E)), and Compose elements with the [`ComposeTestRule`](/reference/kotlin/androidx/compose/ui/test/junit4/ComposeTestRule). \n\n @Test\n fun androidViewInteropTest() {\n // Check the initial state of a TextView that depends on a Compose state.\n Espresso.onView(withText(\"Hello Views\")).check(matches(isDisplayed()))\n // Click on the Compose button that changes the state.\n composeTestRule.onNodeWithText(\"Click here\").performClick()\n // Check the new value.\n Espresso.onView(withText(\"Hello Compose\")).check(matches(isDisplayed()))\n }\n\nInteroperability with UiAutomator\n---------------------------------\n\nBy default, composables are accessible from [UiAutomator](/training/testing/other-components/ui-automator) only by their\nconvenient descriptors (displayed text, content description, etc.). If you want\nto access any composable that uses [`Modifier.testTag`](/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).testTag(kotlin.String)), you need to enable\nthe semantic property `testTagsAsResourceId` for the particular composable's\nsubtree. Enabling this behavior is useful for composables that don't have any\nother unique handle, such as scrollable composables (for example, `LazyColumn`).\n| **Note:** This feature is available in Jetpack Compose version 1.2.0-alpha08 and higher.\n\nEnable the semantic property only once high in your composables hierarchy to\nensure all nested composables with `Modifier.testTag` are accessible from\nUiAutomator. \n\n Scaffold(\n // Enables for all composables in the hierarchy.\n modifier = Modifier.semantics {\n testTagsAsResourceId = true\n }\n ){\n // Modifier.testTag is accessible from UiAutomator for composables nested here.\n LazyColumn(\n modifier = Modifier.testTag(\"myLazyColumn\")\n ){\n // Content\n }\n }\n\nAny composable with the `Modifier.testTag(tag)` can be accessible with the use\nof [`By.res(resourceName)`](/reference/androidx/test/uiautomator/BySelector#res) using the same `tag` as the `resourceName`.\n**Caution:** Make sure you don't use [`By.res(resourcePackage, resourceId)`](/reference/androidx/test/uiautomator/BySelector#res_1) as this formats the argument as `$resourcePackage:id/$resourceId`, which is different from `Modifier.testTag`. \n\n val device = UiDevice.getInstance(getInstrumentation())\n\n val lazyColumn: UiObject2 = device.findObject(By.res(\"myLazyColumn\"))\n // Some interaction with the lazyColumn.\n\nAdditional Resources\n--------------------\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."]]