API นี้มีขนาดเล็กและต้องตั้งค่าน้อยลง คุณจึงสามารถทำการพัฒนาที่ขับเคลื่อนด้วยการทดสอบขณะพัฒนาวิดเจ็ตแต่ละส่วนและจัดระเบียบเพื่อปรับปรุงการนำโค้ดกลับมาใช้ใหม่ได้
// Other Glance and Compose runtime dependencies....testImplementation'androidx.glance:glance-testing:1.1.1'testImplementation'androidx.glance:glance-appwidget-testing:1.1.1'testImplementation'org.robolectric:robolectric:4.11.1'...// You may include additional dependencies, such as Robolectric, if your test// needs to set a LocalContext.
classMyGlanceComposableTest{@TestfunmyNewsItemComposable_largeSize_hasAuthorAsSubtitle()=runGlanceAppWidgetUnitTest{// Prepare inputs and statesetAppWidgetSize(100.dp,100.dp)// Set the composable under testprovideComposable{MyNewsItemComposable(TEST_NEWS_ITEM)}// Perform assertionsonNode(hasTestTag("subTitle")).assertHasText(TEST_NEWS_ITEM.authorName)}}
[[["เข้าใจง่าย","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,["# Unit testing with Glance\n\nThe Glance unit test API let you test your Glance code without inflating views\nor needing a UI automator. For example, the unit test API lets you verify\nconditions, such as whether elements are in a list or whether boxes have been\nchecked, using matchers such as `hasContentDescriptionEqualTo` or `isChecked`.\n\nThis API is lightweight and requires less setup, so you can perform test driven\ndevelopment as you develop individual pieces of your widget and organize them to\nimprove code reuse.\n| **Note:** The test doesn't render the composables under test, so it doesn't let you perform clicks. Instead, it uses matchers, which let you perform assertions on actions in clickables such as starting a service or activity.\n\nSetup\n-----\n\nThe dependencies required to use the unit test library are shown in the\nfollowing examples: \n\n // Other Glance and Compose runtime dependencies.\n ...\n testImplementation 'androidx.glance:glance-testing:1.1.1'\n testImplementation 'androidx.glance:glance-appwidget-testing:1.1.1'\n testImplementation 'org.robolectric:robolectric:4.11.1'\n ...\n // You may include additional dependencies, such as Robolectric, if your test\n // needs to set a LocalContext.\n\nTest structure\n--------------\n\nOrganize composable functions outside of the `GlanceAppWidget` class to enable\ncode reuse and unit testing. Reduce the complexity of your units under test as\nmuch as possible. \n\n class MyGlanceComposableTest {\n @Test\n fun myNewsItemComposable_largeSize_hasAuthorAsSubtitle() = runGlanceAppWidgetUnitTest {\n // Prepare inputs and state\n setAppWidgetSize(100.dp, 100.dp)\n\n // Set the composable under test\n provideComposable {\n MyNewsItemComposable(TEST_NEWS_ITEM)\n }\n\n // Perform assertions\n onNode(hasTestTag(\"subTitle\"))\n .assertHasText(TEST_NEWS_ITEM.authorName)\n }\n }\n\nSet context and size for the test\n---------------------------------\n\nIf your composable function reads context using the `LocalContext.current()`\nmethod, you must set a context using `setContext()`. Otherwise, this step is\noptional.\n\nYou can use any JVM-based Android unit testing framework, such as Roboletric, to\nprovide the context.\n\nIf your composable function accesses `LocalSize`, set the intended size\nfor the test before providing a composable in the test. The default size is\n349.dp x 455.dp, which is equivalent to a 5x4 widget shown on a Pixel 4 device\nin portrait mode.\n\n- If your AppWidget uses `sizeMode == Single`, you can set this to the `minWidth` and `minHeight` in your widget's `info.xml` file.\n- If your AppWidget uses `sizeMode == Exact`, you can identify the sizes to test in a similar way to how you [determine a size for your widget](/develop/ui/views/appwidgets/layouts#anatomy_determining_size) and identify landscape and portrait sizes that your widget may appear on and test for them.\n- If your AppWidget uses `sizeMode == Responsive`, you can set this to one of the sizes from the list that you provide when specifying the `sizeMode`.\n\nThe default duration for a test timeout is 1 second, but you can pass a custom\nduration as an argument to the `runGlanceAppWidgetUnitTest` method if your test\ninfrastructure enforces a different timeout.\n\nFor more information and code samples, see the reference documentation for\n[`runGlanceAppWidgetUnitTest`](/reference/kotlin/androidx/glance/appwidget/testing/unit/package-summary#runGlanceAppWidgetUnitTest(kotlin.time.Duration,kotlin.Function1))."]]