Espresso with a Jetpack: How to test an app with Espresso, that has bits of Jetpack Compose?

espresso and jetpack

17 April 2023 stoefln Leave a comment Espresso

You might have an app that is built with the standard Android SDK, but has a couple of fragments that are built with Jetpack Compose.
Your first approach might be to create a ComposeTestRule, but it will generate a blank ComponentActivity and display an empty screen.

The problem

@get:Rule
val composeTestRule = createComposeRule()

This is a problem. You want to integrate with an existing Espresso test, so this approach won’t be suitable.

Alternatively, you could use createAndroidComposeRule(), but this will also rely on an ActivityTestRule underneath and launch the Activity, potentially altering the behavior of your existing test. 🤔

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

The solution

The solution is to use createEmptyComposeRule() . It will keep the Espresso test the same way it is, but also interact with some compose elements:

@get:Rule
val composeTestRule = createEmptyComposeRule()

Alternative approach

You can also use a framework-agnostic mobile test automation tool such as Repeato to get your job done. Repeato does not care about which technology you used to built your app.

Check out our getting-started video to get an idea of how it’s done.

Tags: ,

Like this article? there’s more where that came from.