CheckOps home

Tools directory

J

CheckOps

Jetpack Compose UI testing

Jetpack Compose's testing APIs (ui-test-junit4) let Android developers find, assert on and interact with composables through semantics, using createComposeRule on devices or with Robolectric on the JVM.

Visit website

Latest public signals

Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.

Visit Website

Introduction

Jetpack Compose, Android’s modern UI toolkit, has its own testing APIs. Google’s documentation explains the key concepts: semantics give meaning to UI so tests can interact with specific elements; testing APIs let you find elements, verify their attributes and perform user actions; and synchronisation means Compose tests wait for the UI to be idle automatically. The docs also cover a cheatsheet, test configuration and execution, interoperability with views, common patterns and debugging.

Tests use a rule created with createComposeRule() (or createAndroidComposeRule<Activity>() when activity access is needed), set content with setContent { ... }, then find nodes with finders such as onNodeWithText, onNodeWithTag and onNodeWithContentDescription, perform actions like performClick() and performTextInput(), and assert with assertIsDisplayed(), assertTextEquals() and others. The dependencies are androidx.compose.ui:ui-test-junit4 plus ui-test-manifest for createComposeRule. The same tests can run as instrumented tests on devices or as local tests with Robolectric, and frameworks such as Kaspresso and Roborazzi build on these APIs.

For debugging, printToLog() dumps the semantics tree, and the merged versus unmerged tree options explain why a finder matches or misses a node.

Features

  1. Semantics-based finders: Find nodes by text, tag or description.
  2. Automatic synchronisation: Waits for Compose to be idle.
  3. Rich assertions and actions: Click, input, scroll and assert.
  4. Device or JVM: Run on emulators or with Robolectric.

Pros

  1. Free: Part of AndroidX.
  2. Built-in sync: Less flakiness from timing issues.

Cons

  1. Semantics required: Custom components need proper semantics or test tags.
  2. Compose only: View-based screens need Espresso.
androidTestImplementation("androidx.compose.ui:ui-test-junit4:<compose_version>")
debugImplementation("androidx.compose.ui:ui-test-manifest:<compose_version>")
class CounterTest {
    @get:Rule val composeTestRule = createComposeRule()

    @Test
    fun incrementsCounter() {
        composeTestRule.setContent { Counter() }
        composeTestRule.onNodeWithText("Increment").performClick()
        composeTestRule.onNodeWithTag("count").assertTextEquals("1")
    }
}

Resources:

Pricing

Free and open source as part of Jetpack Compose (Apache-2.0).

  1. Android teams using Jetpack Compose.
  2. Teams wanting fast Compose tests on the JVM via Robolectric.

Conclusion

Compose’s testing APIs make UI tests concise and reliable, and they are the foundation for most modern Android UI testing.

Alternatives

Other tools on CheckOps that cover similar ground. Open a page, or compare one with Jetpack Compose UI testing side by side.