-
Notifications
You must be signed in to change notification settings - Fork 6.3k
UI Testing with Espresso
Espresso is an instrumentation test framework...
-
The first thing we should do is change to the
Project
perspective in theProject Window
. This will show us a full view of everything contained in the project. The default setting (theAndroid
perspective) hides certain folders: -
Next, open the
Build Variants
window and make sure theTest Artifact
is set toAndroid Instrumentation Tests
. Without this, our tests won't be included in the build. -
Make sure you have an
app/src/androidTest/java
folder. This is the default location for instrumentation tests. -
You'll also need to make sure you have the
Android Support Repository
version 15+ installed. -
It's recommended to turn off system animations on the device or emulator we will be using. Since Espresso is a UI testing framework, system animations can introduce flakiness in our tests. Under
Settings
=>Developer options
disable the following 3 settings and restart the device: -
Finally, we need to pull in the Espresso dependencies and set the test runner in our app build.gradle:
// build.gradle
...
android {
...
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
...
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.3') {
// Necessary if your app targets Marshmallow (since the test runner
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
}
That's all the setup needed. Now let's move on to writing some actual tests.
The code below shows a simple Espresso test that verifies that a single TextView
exists on screen containing the text "Hello world!". It's based off the standard new project template which has a single MainActivity
that contains a TextView
with the text "Hello world!".
Create a new class MainActivityInstrumentationTest
inside of the default instrumentation tests directory (src/androidTest/java
). The best practice is to mimic the same package structure with your tests as your product code. This has the benefit of giving your tests access to package-private
fields in your product code. For this example, we'll be creating MainActivityInstrumentationTest
at src/androidTest/java/com.codepath.espressodemo.MainActivityInstrumentationTest
.
// MainActivityInstrumentationTest.java
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
// Tests for MainActivity
public class MainActivityInstrumentationTest {
// Preferred JUnit 4 mechanism of specifying the activity to be launched before each test
@Rule
public ActivityTestRule<MainActivity> activityTestRule =
new ActivityTestRule<>(MainActivity.class);
// Looks for a single TextView with the text "Hello world!"
// This TextView's bounds must be 90%+ visible on screen (with Visibility = View.VISIBLE)
// for the test to pass.
@Test
public void validateHelloWorldExists() {
onView(withText("Hello world!")).check(matches(isDisplayed()));
}
}
There are 2 ways to run your tests:
- Run a single test through Android Studio:
-
Right click on the test class and select
Run
: -
Note: If you are presented with two options as in the diagram below, make sure to select the first one (designating to use the Gradle Test Runner instead of the JUnit Test Runner). Android Studio will cache your selection for future runs.
-
View the results in the
Console
output. You may need to enableShow Passed
as in the diagram below to see the full results.
- Run all the tests through Gradle:
-
Open the Gradle window and find
connectedDebugAndroidTest
underTasks
=>verification
. -
Right click and select
Run
-
This will generate an html test result report at
app/build/reports/androidTests/connected/index.html
-
Note: You can also run the tests on the command line using:
./gradlew connectedDebugAndroidTest
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.