Espresso Testing and Screen Robots: Getting Started
In this Espresso testing tutorial you’ll learn how to create UI tests with Espresso and how to leverage Screen Robots to make them clear and maintainable. By Victoria Gonda.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Espresso Testing and Screen Robots: Getting Started
25 mins
Testing your app is immensely important, but often overlooked. Sometimes this is because the developer simply doesn’t know where to start. One place you can start is by testing your UI using Espresso tests.
You can further power your Espresso test writing by using Screen Robots to take care of repetitive testing code.
In this tutorial, you will learn:
- What UI tests are and why they are important.
- How to use Espresso to test your UI.
- What Screen Robots are and how they can be helpful.
Using Espresso
Espresso is not only a delicious coffee beverage but also an entire testing framework from Google that allows you to test the UI of your Android app. Manually click-testing all parts of your app is way too slow and tedious. With Espresso, you can write automated tests to make sure everything works as expected and catch regressions before they happen.
You can speed up your test writing even more by creating Screen Robots. These are helper functions that take care of repetition you may have in your tests, such as filling out a form.
Getting Started
To learn about UI testing, you will write tests for an Emoji Tip Calculator app. This app allows you to enter the total from your bill, pick an emoji that represents how you feel about the service and get a tip amount along with the total. You also have the option to round up to the nearest dollar.
Start by downloading the sample project using the Download Materials button at the top or bottom of this tutorial. Open and build the starter project in Android Studio.
Run the app and try it out so that you know the expected behaviors. Try different dollar amounts, pick different emojis and toggle the Round up to nearest dollar switch to see what happens.
You’ll write a whole new test file for this tutorial, but it may be helpful to become familiar with some of the other files. Take a look at MainActivity.kt and activity_main.xml. These define the views and logic for the app.
Adding Dependencies
By default, a generated Android project includes some Espresso dependencies. Every new project should already include the following dependencies in app/build.gradle:
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
And a default test runner in the defaultConfig
:
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
This should be enough for you to write UI tests, however, you will add more rule and truth assertion libraries provided by Google to reduce the boilerplate code required in tests. Add the following dependencies to app/build.gradle:
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
Sync your .gradle files after you make that change. You can see a breakdown of what each testing dependancies provides in the Google documentation.
Writing Espresso Tests
First, you need a file in which to write your tests. To see the actual file structure of the project, switch to the Project view by selecting Project from the dropdown at the top of the Project window on the left.
Create a MainActivityTest.kt under app ‣ src ‣ androidTest ‣ java ‣ com ‣ raywenderlich ‣ android ‣ emojicalculator. It’s standard practice to put your test file under the same package structure as the file you’re testing, replacing main with androidTest, or test for non Android tests. This package structure mimics the location of MainActivity.kt, as that’s what you’re testing.
Add the empty class to your file:
class MainActivityTest {
}
Keep the defaults and select OK.
Then, choose the androidTest directory and select OK again.
MainActivity
, press ⌘ + ⇧ + T (or Control + Shift + T on Windows) and select Create New Test…. A window will pop up to select the location of the test.
Keep the defaults and select OK.
Finally, before writing your tests, you need to add an annotation to say what to run the tests with. Add this right above your test class declaration.
@RunWith(AndroidJUnit4::class)
Use androidx.test.ext.junit.runners.AndroidJUnit4
when prompted to import AndroidJUnit4
and org.junit.runner.RunWith
to import RunWith
.
This tells JUnit to run the tests inside of AndroidJUnit4
instead of the runner built into JUnit. AndroidJUnit4
is a test runner that runs JUnit style tests on Android devices. When used in Espresso tests, it controls launching the app and running UI tests.
In addition to removing that dependency, add this TestRule
inside your MainActivityTest
:
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
In addition to removing that dependency, add this TestRule
inside your MainActivityTest
:
@get:Rule
val activityRule = ActivityTestRule(MainActivity::class.java, false, false)
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
@get:Rule
val activityRule = ActivityTestRule(MainActivity::class.java, false, false)
Launching an Activity
When testing your UI, the first thing you need to make sure you can do is launch it! A launch test is a great first test and makes sure your app, or a specific screen, can start up without crashing. That’s important!
Test names should be clear, easy to read and should focus on telling what they are supposed to do. To achieve that, you should avoid using the word test in your test name.
Write a test that launches the MainActivity
in your test class:
@Test
fun appLaunchesSuccessfully() {
ActivityScenario.launch(MainActivity::class.java)
}
This uses the ActivityScenario
to launch the MainActivity
. The ActivityScenario
class is a part of AndroidX and provides APIs to start and drive an Activity’s lifecycle state for testing. Use the androidx.test.core.app.ActivityScenario
package when prompted to import ActivityScenario
and org.junit.Test
to import Test
. The test will automatically fail if the app crashes for some reason.
ActivityScenario.launch()
with the following anytime you need to launch the activity:
activityRule.launchActivity(null)
activityRule.launchActivity(null)
Before you run your test, you should turn off animations on your testing device. Whether it’s an emulator or physical device you’re working with, go to Settings ‣ Developer options and set all the following to off:
- Window animation scale.
- Transition animation scale.
- Animator duration scale.
Build and run your test! You can do this in one of a number of ways:
- Select the green triangle in the gutter of the test file.
- Right-click on the test class declaration or the file name in the navigator and select Run ‘MainActivityTest’.
- Running
./gradlew connectedAndroidTest
from the command line. - Set up a test configuration as described here.
throw RuntimeException()
some place in onCreate()
of MainActivity
. It’s good to see your test fail: That’s how you know it’s doing its job. Just make sure you remember to fix it!