Setup your test environment
To avoid flakiness, we highly recommend that you turn off system animations on the virtual or physical device(s) used for testing.
On your device, under Settings->Developer options disable the following 3 settings:
Download Espresso
Make sure you have installed the latest Android Support Repository under Extras (see instructions).
Open your app’s build.gradle file. This is usually not the top-level build.gradle file but app/build.gradle.
Add the following lines inside dependencies:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' androidTestCompile 'com.android.support.test:runner:0.5'
See the downloads section for more artifacts (espresso-contrib, espresso-web, etc.)
Set the instrumentation runner
Add to the same build.gradle file the following line in android.defaultConfig:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Example build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22"
defaultConfig {
applicationId "com.my.awesome.app"
minSdkVersion 10
targetSdkVersion 22.0.1
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
// App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
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 passing the following argument to the test runner: disableAnalytics "true" (see how to pass custom arguments).
Add the first test
Android Studio creates tests by default in src/androidTest/java/com.example.package/
Example JUnit4 test using Rules:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
@Test
public void listGoesOverTheFold() {
onView(withText("Hello world!")).check(matches(isDisplayed()));
}
}
Running tests
In Android Studio
Create a test configuration
In Android Studio:
Open Run menu -> Edit Configurations
Add a new Android Tests configuration
Choose a module
Add a specific instrumentation runner:
android.support.test.runner.AndroidJUnitRunner
Run the newly created configuration.
From command-line via Gradle
Execute
./gradlew connectedAndroidTest
Espresso has basically three components:
ViewMatchers - allows to find view in the current view hierarchy
ViewActions - allows to perform actions on the views
ViewAssertions - allows to assert state of a view
Base Espresso Test
onView(ViewMatcher) -- 1
.perform(ViewAction) -- 2
.check(ViewAssertion); -- 3