New

๐Ÿงช Jetpack Compose Testing Tutorial for Beginners

Write your first Compose UI tests: set up ui-test-junit4, find nodes, type and click, test a LazyColumn and control the clock with the v2 testing APIs.

Beginner โฑ๏ธ ~35 minutes ๐Ÿงฐ Kotlin, Jetpack Compose 1.13, JUnit4
โ† Android Track

What You'll Learn in This Jetpack Compose Testing Tutorial

This Jetpack Compose testing tutorial shows you how to check your UI automatically instead of tapping through the app after every change. You will test a small greeting screen and a scrolling list with Compose UI tests: find elements, type text, click buttons, scroll and assert what the user should see.

Searches for "jetpack compose testing", "compose ui testing" and "jetpack compose test tag" keep growing as more teams move to Compose, and UI testing is a common topic in Android interviews. Everything here uses the current v2 testing APIs, which Google now recommends over the deprecated v1 versions.

Prerequisites

Compose UI Testing, Step by Step

1 Add the Compose testing dependencies

Open your app module's build.gradle.kts. The Compose BOM keeps every Compose library, including the test ones, on matching versions. We use the November 2026 BOM, 2026.11.01, which brings Compose 1.13; if your project uses another BOM, keep your version.

android {
    defaultConfig {
        // Runs instrumented tests in src/androidTest
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    val composeBom = platform("androidx.compose:compose-bom:2026.11.01")
    implementation(composeBom)
    androidTestImplementation(composeBom)

    // The Compose test APIs (createComposeRule, finders, assertions)
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    // Adds the empty activity that createComposeRule launches
    debugImplementation("androidx.compose.ui:ui-test-manifest")
    androidTestImplementation("androidx.test.ext:junit:1.3.0")
}

Two lines matter here. ui-test-junit4 gives you the test rule, finders and assertions. ui-test-manifest goes into debugImplementation, not androidTestImplementation, because it adds an empty activity to your debug app that the test rule launches. Sync Gradle when you are done.

2 Write a small screen to test

Testing is easiest when you test one composable at a time. Here is a greeting screen: a text field, a button that stays disabled until you type a name, and a greeting that appears after you click.

// app/src/main/java/com/example/composetesting/GreetingScreen.kt
package com.example.composetesting

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp

@Composable
fun GreetingScreen(modifier: Modifier = Modifier) {
    var name by rememberSaveable { mutableStateOf("") }
    var greeting by rememberSaveable { mutableStateOf<String?>(null) }

    Column(modifier.padding(16.dp)) {
        OutlinedTextField(
            value = name,
            onValueChange = { name = it },
            label = { Text("Your name") },
            singleLine = true,
            modifier = Modifier.testTag("nameField")
        )
        Button(
            onClick = { greeting = "Hi, ${name.trim()}!" },
            enabled = name.isNotBlank(),
            modifier = Modifier.padding(top = 8.dp)
        ) {
            Text("Say hi")
        }
        greeting?.let {
            Text(it, modifier = Modifier.testTag("greeting"))
        }
    }
}

Modifier.testTag gives a node a name that tests can find. You only need tags where the text is not unique or not known in advance; the button can be found by its label.

3 Write your first Compose UI test

Create the test in src/androidTest. The rule from createComposeRule() starts an empty activity, and setContent puts your composable in it. Note the v2 import: Google deprecated the v1 testing APIs, and the v2 versions are what new code should use.

// app/src/androidTest/java/com/example/composetesting/GreetingScreenTest.kt
package com.example.composetesting

import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class GreetingScreenTest {

    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun sayHiButton_isDisabled_whenNameIsEmpty() {
        composeTestRule.setContent { GreetingScreen() }

        composeTestRule.onNodeWithText("Say hi").assertIsNotEnabled()
    }
}

Run it from the green arrow next to the class in Android Studio, or from a terminal with a device or emulator connected:

# Start an emulator or plug in a phone, then:
./gradlew connectedDebugAndroidTest

Every Compose test follows the same three moves: find a node (onNodeWithText), act on it if needed, then assert something about it (assertIsNotEnabled).

4 Find nodes in the semantics tree

Compose tests do not look at pixels. They look at the semantics tree, the same description of your UI that screen readers such as TalkBack use. The main finders are:

  • onNodeWithText("Say hi") matches a node by its visible text. Pass substring = true or ignoreCase = true when you need looser matching.
  • onNodeWithTag("nameField") matches a Modifier.testTag.
  • onNodeWithContentDescription("Delete") matches icons and images by their content description, which also makes your app more accessible.
  • onAllNodesWithText(...) and onAllNodesWithTag(...) return a collection when several nodes match; use onFirst(), get(index) or assertCountEquals(n) on it.

A single-node finder fails if it finds zero nodes or more than one, which is a good thing: it tells you your test is not specific enough.

5 Act and assert: type, click and check

Add these tests to the same class. They type a name, click the button and check the greeting, then check that nothing shows before the click.

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput

@Test
fun typingName_andClickingSayHi_showsGreeting() {
    composeTestRule.setContent { GreetingScreen() }

    composeTestRule.onNodeWithTag("nameField").performTextInput("Asha")
    composeTestRule.onNodeWithText("Say hi")
        .assertIsEnabled()
        .performClick()

    composeTestRule.onNodeWithTag("greeting")
        .assertIsDisplayed()
        .assertTextEquals("Hi, Asha!")
}

@Test
fun greeting_isHidden_beforeClick() {
    composeTestRule.setContent { GreetingScreen() }

    composeTestRule.onNodeWithTag("nameField").performTextInput("Asha")
    composeTestRule.onNodeWithTag("greeting").assertDoesNotExist()
}

Actions start with perform: performClick(), performTextInput(), performTextClearance(), performScrollTo() and performTouchInput { swipeUp() }. Assertions start with assert: assertIsDisplayed(), assertExists(), assertDoesNotExist(), assertIsEnabled(), assertTextEquals() and assertTextContains(). You do not need to wait or sleep between them; the test rule waits until Compose is idle before each step.

6 Debug a failing finder with printToLog

When a test says it found no node, print the semantics tree and read it in Logcat:

import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.printToLog

// Prints the semantics tree to Logcat under the tag "GREETING"
composeTestRule.onRoot().printToLog("GREETING")

// See every child node instead of the merged ones
composeTestRule.onRoot(useUnmergedTree = true).printToLog("GREETING")

Components such as Button merge their children's semantics into one node, so the button and its "Say hi" label show up as a single clickable node. That is why onNodeWithText("Say hi") can click the button. If you need the child itself, for example an icon inside a button, pass useUnmergedTree = true to the finder.

7 Test a LazyColumn list

Lists need one extra idea: a LazyColumn only composes the rows on screen, so off-screen rows are not in the tree. Scroll to them first.

// main source set
@Composable
fun TopicList(topics: List<String>) {
    LazyColumn(Modifier.testTag("topicList")) {
        items(topics) { topic ->
            Text(topic, Modifier.padding(16.dp).testTag("topicRow"))
        }
    }
}

// androidTest
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.performScrollToNode

@Test
fun topicList_scrollsToLastTopic() {
    val topics = (1..50).map { "Topic $it" }
    composeTestRule.setContent { TopicList(topics) }

    composeTestRule.onAllNodesWithTag("topicRow").onFirst().assertTextEquals("Topic 1")
    // LazyColumn only composes visible rows, so Topic 50 does not exist yet
    composeTestRule.onNodeWithText("Topic 50").assertDoesNotExist()

    composeTestRule.onNodeWithTag("topicList").performScrollToNode(hasText("Topic 50"))
    composeTestRule.onNodeWithText("Topic 50").assertIsDisplayed()
}

performScrollToNode runs on the scrollable list and scrolls until a child matches. For a non-lazy Column with verticalScroll, call performScrollTo() on the item instead.

8 Control time and coroutines

The v2 APIs run composition on a StandardTestDispatcher, the same kind runTest uses. Coroutines you launch are queued until the test moves the virtual clock, so timing bugs show up instead of being hidden. runComposeUiTest is the compact way to write such a test:

import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.v2.runComposeUiTest
import kotlinx.coroutines.delay
// plus the remember, mutableStateOf, LaunchedEffect and Text imports from step 2

@OptIn(ExperimentalTestApi::class)
@Test
fun status_changesAfterOneSecond() = runComposeUiTest {
    setContent {
        var status by remember { mutableStateOf("Loading...") }
        LaunchedEffect(Unit) {
            delay(1000)
            status = "Done!"
        }
        Text(text = status)
    }

    onNodeWithText("Loading...").assertIsDisplayed()
    mainClock.advanceTimeBy(1000 + 16) // one second plus a frame
    onNodeWithText("Done!").assertIsDisplayed()
}

mainClock.advanceTimeBy moves Compose's clock forward without really waiting a second, so animations and delay calls run instantly and the test stays fast. With the rule, you can call composeTestRule.waitForIdle() or composeTestRule.runOnIdle { } after calling a ViewModel function, so queued work finishes before you assert. On older Compose versions runComposeUiTest needed the ExperimentalTestApi opt-in, which is why it is shown here.

Practice Task

Extend the tests on your own, then check yourself with the quiz below.

  1. Write a test that types " " (only spaces) and checks that the Say hi button stays disabled.
  2. Add a Clear button to GreetingScreen that empties the field, and test it with performClick() and assertTextEquals on the greeting.
  3. Give each list row a delete IconButton with contentDescription = "Delete $topic", then find it with onNodeWithContentDescription and check the row disappears.
  4. Move the greeting logic into a ViewModel with a StateFlow, as in the Kotlin Flow tutorial, and test the screen with a fake ViewModel state.

Jetpack Compose Testing Quiz

Five quick questions on this tutorial.

Frequently Asked Questions

What is the difference between createComposeRule and createAndroidComposeRule?

createComposeRule launches an empty activity for you and lets you call setContent with any composable, which is best for testing one screen or component. createAndroidComposeRule<MainActivity>() launches your real activity, which is useful when you need its resources or want to test the screen the activity sets up itself.

Which import should I use for createComposeRule in 2026?

Use androidx.compose.ui.test.junit4.v2.createComposeRule. Google has deprecated the v1 testing APIs. The v2 APIs ship in ui-test-junit4 1.11.0-alpha03 and later, so current Compose BOM versions include them.

Why does my Compose test say no node found?

The finder did not match anything in the semantics tree. Print the tree with onRoot().printToLog("TAG"), check the exact text and case, add a Modifier.testTag to the element, and remember that a LazyColumn does not compose rows that are off screen until you scroll.

Do Compose UI tests need an emulator?

Tests in src/androidTest are instrumented tests, so they run on an emulator or a real device with ./gradlew connectedDebugAndroidTest. Compose tests can also run on the JVM with Robolectric, which is faster but is a separate setup.

What is useUnmergedTree in Compose tests?

Some components, such as Button, merge the semantics of their children into one node. Passing useUnmergedTree = true to a finder lets you match the child nodes, such as an Icon inside a Button, instead of the merged parent.

Sources

APIs checked against the Android Developers documentation on December 6, 2026. Library versions change often; prefer the versions your Android Studio template suggests.

What to Try Next

Keep learning Android

The free Android track takes you from Kotlin basics to complete Compose apps with data, networking, navigation and tests.

Back to the Android Track