This guide covers installing Espresso using the SDK Manager and building it using Gradle. Android Studio is recommended.
Set up your test environment
To avoid flakiness, we highly recommend that you turn off system animations on the virtual or physical devices used for testing. On your device, under Settings > Developer options, disable the following 3 settings:
- Window animation scale
- Transition animation scale
- Animator duration scale
Add Espresso dependencies
To add Espresso dependencies to your project, complete the following steps:
- Open your app’s
build.gradle
file. This is usually not the top-levelbuild.gradle
file butapp/build.gradle
. - Add the following lines inside dependencies:
Groovy
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1' androidTestImplementation 'androidx.test:runner:1.6.1' androidTestImplementation 'androidx.test:rules:1.6.1'
Kotlin
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1') androidTestImplementation('androidx.test:runner:1.6.1') androidTestImplementation('androidx.test:rules:1.6.1')
View the complete set of Gradle dependencies.
Set the instrumentation runner
Add to the same build.gradle
file the following line in
android.defaultConfig
:
Groovy
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Kotlin
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Example Gradle build file
Groovy
plugins { id 'com.android.application' } android { compileSdkVersion 33 defaultConfig { applicationId "com.my.awesome.app" minSdkVersion 21 targetSdkVersion 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } } dependencies { androidTestImplementation 'androidx.test:runner:1.6.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1' }
Kotlin
plugins { id("com.android.application") } android { compileSdkVersion(33) defaultConfig { applicationId = "com.my.awesome.app" minSdkVersion(21) targetSdkVersion(33) versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } } dependencies { androidTestImplementation('androidx.test:runner:1.6.1') androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1') }
Analytics
In order to make sure we are on the right track with each new release, the test runner collects analytics. More specifically, it uploads a hash of the package name of the application under test for each invocation. This allows us to measure both the count of unique packages using Espresso as well as the volume of usage.
If you do not wish to upload this data, you can opt out by including the
disableAnalytics
argument in your instrumentation command:
adb shell am instrument -e disableAnalytics true
Add the first test
Android Studio creates tests by default in
src/androidTest/java/com.example.package/
.
Example JUnit4 test using Rules:
Kotlin
@RunWith(AndroidJUnit4::class) @LargeTest class HelloWorldEspressoTest { @get:Rule val activityRule = ActivityScenarioRule(MainActivity::class.java) @Test fun listGoesOverTheFold() { onView(withText("Hello world!")).check(matches(isDisplayed())) } }
Java
@RunWith(AndroidJUnit4.class) @LargeTest public class HelloWorldEspressoTest { @Rule public ActivityScenarioRule<MainActivity> activityRule = new ActivityScenarioRule<>(MainActivity.class); @Test public void listGoesOverTheFold() { onView(withText("Hello world!")).check(matches(isDisplayed())); } }
Run tests
You can run your tests in Android Studio or from the command line.
In Android Studio
To create a test configuration in Android Studio, complete the following steps:
- Open Run > Edit Configurations.
- Add a new Android Tests configuration.
- Choose a module.
- Add a specific instrumentation runner:
androidx.test.runner.AndroidJUnitRunner
- Run the newly created configuration.
From the command line
Execute the following Gradle command:
./gradlew connectedAndroidTest