Koleksiyonlar ile düzeninizi koruyun
İçeriği tercihlerinize göre kaydedin ve kategorilere ayırın.
Glance birim testi API'si, görüntülemeleri artırmadan veya UI Automator'a ihtiyaç duymadan Glance kodunuzu test etmenizi sağlar. Örneğin, birim testi API'si, hasContentDescriptionEqualTo veya isChecked gibi eşleştiricileri kullanarak öğelerin listede olup olmadığını ya da kutuların işaretlenip işaretlenmediğini doğrulamanıza olanak tanır.
Bu API, hafiftir ve daha az kurulum gerektirir. Bu nedenle, widget'ınızın bağımsız parçalarını geliştirirken ve kodun yeniden kullanımını iyileştirmek için bunları düzenlerken test odaklı geliştirme yapabilirsiniz.
Kurulum
Birim testi kitaplığını kullanmak için gereken bağımlılıklar aşağıdaki örneklerde gösterilmiştir:
// 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.
Test yapısı
Kodun yeniden kullanılmasını ve birim testini etkinleştirmek için birleştirilebilir işlevleri GlanceAppWidget sınıfının dışında düzenleyin. Test edilen birimlerinizin karmaşıklığını mümkün olduğunca azaltın.
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)}}
Test için bağlam ve boyut ayarlama
Composable işleviniz LocalContext.current() yöntemini kullanarak bağlamı okuyorsa setContext() yöntemini kullanarak bir bağlam ayarlamanız gerekir. Aksi takdirde bu adım isteğe bağlıdır.
Bağlam sağlamak için Roboletric gibi JVM tabanlı herhangi bir Android birim testi çerçevesini kullanabilirsiniz.
Composable işleviniz LocalSize öğesine erişiyorsa testte composable sağlamadan önce test için amaçlanan boyutu ayarlayın. Varsayılan boyut 349 dp x 455 dp'dir. Bu boyut, Pixel 4 cihazda dikey modda gösterilen 5x4 boyutlu bir widget'a eşdeğerdir.
AppWidget'ınız sizeMode == Single kullanıyorsa bunu widget'ınızın info.xml dosyasında minWidth ve minHeight olarak ayarlayabilirsiniz.
AppWidget'ınız sizeMode == Exact kullanıyorsa widget'ınız için boyut belirlemeye benzer şekilde test edilecek boyutları belirleyebilir ve widget'ınızın görünebileceği yatay ve dikey boyutları belirleyip bu boyutlar için test yapabilirsiniz.
Test zaman aşımı için varsayılan süre 1 saniyedir ancak test altyapınız farklı bir zaman aşımı uyguluyorsa runGlanceAppWidgetUnitTest yöntemine bağımsız bir süre iletebilirsiniz.
Daha fazla bilgi ve kod örnekleri için runGlanceAppWidgetUnitTest ile ilgili referans belgelerine bakın.
Bu sayfadaki içerik ve kod örnekleri, İçerik Lisansı sayfasında açıklanan lisanslara tabidir. Java ve OpenJDK, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2025-08-21 UTC.
[[["Anlaması kolay","easyToUnderstand","thumb-up"],["Sorunumu çözdü","solvedMyProblem","thumb-up"],["Diğer","otherUp","thumb-up"]],[["İhtiyacım olan bilgiler yok","missingTheInformationINeed","thumb-down"],["Çok karmaşık / çok fazla adım var","tooComplicatedTooManySteps","thumb-down"],["Güncel değil","outOfDate","thumb-down"],["Çeviri sorunu","translationIssue","thumb-down"],["Örnek veya kod sorunu","samplesCodeIssue","thumb-down"],["Diğer","otherDown","thumb-down"]],["Son güncelleme tarihi: 2025-08-21 UTC."],[],[],null,["The 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\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\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\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))."]]