Function

runComposeUiTest

Sets up the test environment, runs the given [test]block and then tears down the test environment.

RunComposeUiTestSample

@OptIn(ExperimentalTestApi::class)
@Sampled
fun RunComposeUiTestSample() = runComposeUiTest {
    var counter by mutableIntStateOf(1)
    setContent {
        Column {
            Text(text = "Count: $counter", modifier = Modifier.testTag("text_tag"))
            Button(onClick = { counter++ }, modifier = Modifier.testTag("button_tag")) {
                Text("Click Me!")
            }
        }
    }

    onNodeWithTag("button_tag").performClick()
    onNodeWithTag("text_tag").assert(hasText("Count: 2"))
}

RunComposeUiTestConfigSample

@OptIn(ExperimentalTestApi::class)
@Sampled
fun RunComposeUiTestConfigSample() =
    runComposeUiTest(
        config =
            ComposeUiTestConfig(
                effectContext = StandardTestDispatcher(),
                runTestContext = StandardTestDispatcher(),
                testTimeout = 30.seconds,
                inputMode = InputMode.Touch,
            )
    ) {
        var counter by mutableIntStateOf(1)
        setContent {
            Column {
                Text(text = "Count: $counter", modifier = Modifier.testTag("text_tag"))
                Button(onClick = { counter++ }, modifier = Modifier.testTag("button_tag")) {
                    Text("Click Me!")
                }
            }
        }

        onNodeWithTag("button_tag").performClick()
        onNodeWithTag("text_tag").assert(hasText("Count: 2"))
    }
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: