Unit testing with Glance

The Glance unit test API let you test your Glance code without inflating views or needing a UI automator. For example, the unit test API lets you verify conditions, such as whether elements are in a list or whether boxes have been checked, using matchers such as hasContentDescriptionEqualTo or isChecked.

This API is lightweight and requires less setup, so you can perform test driven development as you develop individual pieces of your widget and organize them to improve code reuse.


The dependencies required to use the unit test library are shown in the following examples:

// Other Glance and Compose runtime dependencies.
testImplementation 'androidx.glance:glance-testing:1.1.0'
testImplementation 'androidx.glance:glance-AppWidget-testing:1.1.0'
testImplementation 'org.robolectric:robolectric:4.11.1'
// You may include additional dependencies, such as Robolectric, if your test
// needs to set a LocalContext.

Test structure

Organize composable functions outside of the GlanceAppWidget class to enable code reuse and unit testing. Reduce the complexity of your units under test as much as possible.

class MyGlanceComposableTest {
    fun myNewsItemComposable_largeSize_hasAuthorAsSubtitle() = runGlanceAppWidgetUnitTest {
        // Prepare inputs and state
        setAppWidgetSize(100.dp, 100.dp)

        // Set the composable under test
        provideComposable {

        // Perform assertions

Set context and size for the test

If your composable function reads context using the LocalContext.current() method, you must set a context using the LocalContext.current() method. Otherwise, this step is optional.

You can use any JVM-based Android unit testing framework, such as Roboletric, to provide the context.

If your composable function accesses LocalSize, set the intended size for the test before providing a composable in the test. The default size is 349.dp x 455.dp, which is equivalent to a 5x4 widget shown on a Pixel 4 device in portrait mode.

  • If your AppWidget uses sizeMode == Single, you can set this to the minWidth and minHeight in your widget's info.xml file.
  • 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 and identify landscape and portrait sizes that your widget may appear on and test for them.
  • 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.

The default duration for a test timeout is 1 second, but you can pass a custom duration as an argument to the runGlanceAppWidgetUnitTest method if your test infrastructure enforces a different timeout.

For more information and code samples, see the reference documentation for runGlanceAppWidgetUnitTest.