Android Compose: testing button click

Andres Sandoval
2 min readAug 29, 2022

I’m learning how to write a Compose test. The goal: is to show basic test to click a button. Series will show run Compose test using Firebase Test Lab.

  1. Using Android Studio Chipmunk create a new Android project “Compose Activity”. Add below snippet to your MainActivity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

setContent {
val context = LocalContext.current

ComposeUITestTheme {
contentAlignment = Alignment.Center,
modifier = Modifier
) {
onClick = {
Toast.makeText(context, "Hello world", Toast.LENGTH_SHORT).show()
modifier = Modifier.testTag("yourTestTag")
) {
Text(text = stringResource(

2. The new project already comes with the Gradle dependencies for Compose UI testing

androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

3. Create new test class TestButton. Add below code snippet and import dependencies.

class TestButton {
val composeTestRule = createAndroidComposeRule(

fun testButtonClick() {
val button = composeTestRule.onNode(hasTestTag("yourTestTag"), useUnmergedTree = true)

4. Run test. The test is going to click the button and the toast is going to show.

Thanks for reading.



Compose testing Google documentation:

Hello world test:

More advance compose Code lab: