ทดสอบเลย์เอาต์ของการเขียน
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ทดสอบ UI ของแอปเพื่อยืนยันว่าลักษณะการทำงานของโค้ด Compose
ถูกต้อง ซึ่งจะช่วยให้คุณพบข้อผิดพลาดได้ตั้งแต่เนิ่นๆ และปรับปรุงคุณภาพของแอปได้
Compose มีชุด API สำหรับการทดสอบเพื่อค้นหาองค์ประกอบ ยืนยันแอตทริบิวต์ และดำเนินการของผู้ใช้ นอกจากนี้ API ยังมีฟีเจอร์ขั้นสูง
เช่น การจัดการเวลา ใช้ API เหล่านี้เพื่อสร้างการทดสอบที่แข็งแกร่งซึ่งจะยืนยัน
ลักษณะการทำงานของแอป
ยอดดู
หากคุณใช้ View แทน Compose โปรดดูส่วนทดสอบแอปใน Android ทั่วไป
โดยเฉพาะอย่างยิ่ง จุดเริ่มต้นที่ดีคือคำแนะนำทดสอบ UI โดยอัตโนมัติ โดยจะอธิบายวิธีทำให้การทดสอบที่ทำงานในอุปกรณ์เป็นแบบอัตโนมัติ รวมถึงเมื่อใช้
มุมมอง
หัวข้อสำคัญ
แนวคิดสำคัญบางประการสำหรับการทดสอบโค้ด Compose มีดังนี้
- ความหมาย: ความหมายจะให้ความหมายแก่ UI ซึ่งช่วยให้การทดสอบโต้ตอบกับองค์ประกอบที่เฉพาะเจาะจงได้
- การทดสอบ API: การทดสอบ API ช่วยให้คุณค้นหาองค์ประกอบ ยืนยันแอตทริบิวต์
และดำเนินการของผู้ใช้ได้
- การซิงโครไนซ์: การซิงโครไนซ์จะตรวจสอบว่าการทดสอบรอให้ UI ไม่ได้ใช้งานก่อนที่จะดำเนินการหรือทำการยืนยัน
- ความสามารถในการทำงานร่วมกัน: ความสามารถในการทำงานร่วมกันช่วยให้การทดสอบทำงานได้กับทั้งองค์ประกอบที่อิงตาม Compose และ View ในแอปเดียวกัน
ชีตโกงสำหรับการทดสอบ
ดูชีตสรุปการทดสอบเพื่อดูภาพรวมของหัวข้อสำคัญทั้งหมดที่คุณควร
เรียนรู้เกี่ยวกับการทดสอบใน Compose
ตั้งค่า
ตั้งค่าแอปเพื่อให้คุณทดสอบโค้ด Compose ได้
ก่อนอื่น ให้เพิ่มการอ้างอิงต่อไปนี้ลงในไฟล์ build.gradle
ของโมดูล
ที่มีการทดสอบ UI
// 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")
โมดูลนี้มี ComposeTestRule
และการติดตั้งใช้งานสำหรับ Android
ที่ชื่อ AndroidComposeTestRule
คุณตั้งค่าการเขียนเนื้อหาหรือเข้าถึงกิจกรรมได้ผ่านกฎนี้ คุณสร้างกฎโดยใช้ฟังก์ชัน Factory
ไม่ว่าจะเป็น createComposeRule
หรือหากต้องการเข้าถึงกิจกรรม
createAndroidComposeRule
การทดสอบ UI ทั่วไปสำหรับ Compose มีลักษณะดังนี้
// 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()
}
}
แหล่งข้อมูลเพิ่มเติม
- ทดสอบแอปใน Android: หน้า Landing Page หลักของการทดสอบ Android
จะให้มุมมองที่กว้างขึ้นเกี่ยวกับพื้นฐานและเทคนิคการทดสอบ
- หลักพื้นฐานของการทดสอบ: ดูข้อมูลเพิ่มเติม
เกี่ยวกับแนวคิดหลักเบื้องหลังการทดสอบแอป Android
- การทดสอบในเครื่อง: คุณสามารถเรียกใช้การทดสอบบางอย่าง
ในเครื่องบนเวิร์กสเตชันของคุณเองได้
- การทดสอบแบบมีเครื่องควบคุม: คุณควรเรียกใช้การทดสอบแบบมีเครื่องควบคุมด้วย กล่าวคือ การทดสอบที่ทำงานโดยตรง
ในอุปกรณ์
- การรวมอย่างต่อเนื่อง:
การรวมอย่างต่อเนื่องช่วยให้คุณผสานรวมการทดสอบเข้ากับไปป์ไลน์การติดตั้งใช้งานได้
- ทดสอบขนาดหน้าจอต่างๆ: เนื่องจากผู้ใช้มีอุปกรณ์ให้เลือกมากมาย คุณจึงควรทดสอบขนาดหน้าจอต่างๆ
- Espresso: แม้ว่า Espresso จะออกแบบมาสำหรับ UI ที่อิงตาม View แต่ความรู้เกี่ยวกับ Espresso ก็ยังเป็นประโยชน์สำหรับบางแง่มุมของการทดสอบ Compose
Codelab
ดูข้อมูลเพิ่มเติมได้ที่โค้ดแล็บการทดสอบ Jetpack Compose
ตัวอย่าง
แนะนำสำหรับคุณ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-21 UTC
[[["เข้าใจง่าย","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-08-21 UTC"],[],[],null,["# Test your Compose layout\n\nTest 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-----\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------------\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------------------\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-----\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\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-------\n\nTo learn more, try the [Jetpack Compose Testing codelab](/codelabs/jetpack-compose-testing).\n\n### Samples\n\nRecommended for you\n-------------------\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)"]]