Skip to content

Conversation

cartland
Copy link
Contributor

    // file: app/src/androidTest/java/com/package/MyComposeTest.kt

class MyComposeTest {

    @get:Rule val composeTestRule = createComposeRule()
    // use createAndroidComposeRule<YourActivity>() if you need access to
    // an activity

    @Test
    fun myTest() {
        // Start the app
        composeTestRule.setContent {
            MyAppTheme {
                MainScreen(uiState = fakeUiState, /*...*/)
            }
        }

        composeTestRule.onNodeWithText("Continue").performClick()

        composeTest-rule.onNodeWithText("Welcome").assertIsDisplayed()
    }
}
  • New snippet
class MyComposeTest {

    @get:Rule
    val composeTestRule = createAndroidComposeRule<ComponentActivity>()

    @Test
    fun myTest() {
        // Start the app
        composeTestRule.setContent {
            MyAppTheme {
                MainScreen(uiState = exampleUiState)
            }
        }
        composeTestRule.onNodeWithText("Continue").performClick()

        composeTestRule.onNodeWithText("Welcome").assertIsDisplayed()
    }
}
  • New snippet file
    • A new, compilable, code snippet in this file: compose/snippets/src/androidTest/java/com/android/example/compose/testing/MyComposeTest.kt
  • Modified files
    • compose/snippets/src/androidTest/java/com/android/example/compose/testing/MyComposeTest.kt
    • compose/snippets/src/main/res/values/strings.xml
  • Region tag
    • The code snippet is included in new // START and // END region tags. The new region tag is android_snippets_compose_testing_common_patterns and follows the format: android_snippets_<module>_<feature>
  • Related code
    • The code snippet is located in a new file that is well named and located.
  • Running the code snippet
    • The code can be run by executing the myTest test in the MyComposeTest class. This is an instrumentation test and can be run with the command ./gradlew :compose:snippets:connectedDebugAndroidTest.
  • Git branch status
    • All changes have been committed to the current branch: cartland/ComposeTest.
  • git status and git diff main
    • We confirmed with git status, git log -n 1, and git diff main that the correct changes have been committed
  • Next steps
    • Review the code in the current branch cartland/ComposeTest
    • When you are ready to make the branch public, run
      • git push
    • Then you can create a pull request at https://github.com/android/snippets/pull/new/cartland/ComposeTest

Copy link

snippet-bot bot commented Aug 22, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant