Espresso is an open source tool that lets you create and run unit tests for Android. Android Studio can record interactions with your app and then play them back, helping you find and fix UI-based bugs.
- [Instructor] Espresso is an open source library that lets you create and run unit tests on Android apps. Specifically, it tests the user interface of an Android application. And in Android Studio you can now record an Espresso test and play it back. I'll demonstrate this by creating a brand new Android Studio project that I'll call EspressoTest. I'll place the project in my Android StudioProjects directory and I'll select the Basic Activity template.
I'll click Next and Finish and that creates the project. Now I need something to test, some change that happens in the application as the result of an end user interaction. In this project my MainActivity class uses an XML layout file and this XML layout file has a FloatingActionButton that has an id of fab, standing for FloatingActionButton. And the MainActivity class already has event handling code that executes some code when I touch that button.
Also, the content_main.xml file has a TextView component. When the user clicks the button I'll change the text that's being displayed by that TextView. To make that happen I'll add an id attribute and I'll call it welcome_message, and I'll make this text a little bit larger by setting a textSize attribute, and I'll set it to 24sp. And I'll take a look at that in Design view and that's entirely visible, that's great. And then I'll go back to the MainActivity class and I'll replace the code in my OnClickListener.
I'll expand this code and I'll be changing the code in the OnClick method, I'll delete that default code, and I'll get a reference to my TextView component, I'll just name it tv, and I'll get its reference by calling findViewById, and passing R.id.welcome_message. And I'll be sure to return the value cast as a TextView object. Then I'll change the text that's being displayed with tv.setText, and I'll pass in Welcome to Espresso!.
Now before I do any tests with this I'll just do a visual test. I'll run the application on a virtual device. And I'm placing the virtual device over here, so I can see both Android Studio and the device at the same time. When I click on the button I see the text Welcome to Espresso!. So far so good. So now I want to record an Espresso test. This is made possible by the inclusion of the Espresso library in the default Gradle build file.
I'll open that file and show that there's an androidTextCompile directive that's referring to the espresso-core library. So now I'll go to the menu and select Run, Record Espresso Test. You have to run the application on a device and it can either be a virtual or a physical device. I'll select it and click OK. And after a moment I see the application open in debugging mode on the device. And I also see a new Record Your Test dialog.
Before I do anything with the application I want to add an assertion. An assertion looks for a particular condition. Whether an object exists, whether a bit of text is displayed, and many others. After a moment I see my activity displayed in the dialog and now I need to select an element that I want to test. I'm going to choose the FloatingActionButton. And over here I see a list of possible conditions I can test with that particular object.
My assertion is that this object exists. I'll click Save Assertion and that shrinks the dialog back down, then I'll bring the Emulator back, and now I'm ready for a user interaction. I'll click on that FloatingActionButton, and I'll wait a moment until I see Welcome to Espresso! is displayed. And over here I see a new item has been added to my script, Tap FloatingActionButton with ID fab. I can visually see that Welcome to Espresso! is being displayed, and I'll add another assertion.
Once again, the activity is displayed in the dialog, and this time I'll click on the TextView component, and the test I'm running is to make sure that the text equals Welcome to Espresso!. I'll save the assertion. And that's the end of my test. I'll click OK. I'm prompted for the name of a Java class that I want to create, and I'll accept the default, MainActivityTest, and click OK. And here's the new Java class.
It's recorded all of the actions that I executed during the test and my assertions. The first bit of code here is an assertion that the floatingActionButton exists and is displayed. The next bit of code is clicking on the floatingActionButton. And the third bit of code is looking to make sure that that textView displays the expected text. Now to run the test I'll go to my Project window and locate the Test class. I'll right-click on it and choose Run MainActivityTest.
I'll choose the device I want to test on, then very quickly come back to the device, so we can see what happens. You'll see that the app loads and then almost instantly goes away. And that's how long it takes to run the test. And over here in Android Studio I se the message that the test passed. But now let's see what happens if I make a change to my code. I'll come back to the MainActivity class, and I'll change the text that I'm displaying, so I'm no longer displaying an !.
And then once again, I'll run my test. I'll right-click on the class and choose Run, and I'll select my deployment target, and then very quickly switch over, and watch what happens. Then I'll come back and see 1 test failed down here. I'll double-click the top of that window to expand it, so I can see all the output. And I see right here, No views in the hierarchy found matching, and it's telling me that it couldn't find a view that matched the id, and the output text, Welcome to Espresso with the !.
I'll come back to my Java class and I'm going to remove one of these conditions. I'm really only interested in testing an object that has the appropriate id, so I'm going to get rid of this condition, withText Welcome to Espresso!. And now I'm only looking for the object with the appropriate id in the appropriate position. I'll come back to the Project window, right-click on the Test class again, and Run it again. And this time I'll just stay in Android Studio.
And once again I see 1 test failed, I'll expand the window, and this time my error is a little bit different. I could find the TextView component, but I'm told that I expected the text Welcome to Espresso with the ! and it wasn't found. So that's a very brief introduction to Espresso. Espresso's been around for a while, but previously you had to hand code a lot of it. Using the Espresso recorder you can get started quickly recording your tests and then quickly evaluating the results.
- Installing Android Studio on Mac and Windows
- Creating Android Studio projects
- Setting up the development environment, including HAXM and the new Android emulator
- Importing existing code into Android Studio projects
- Exploring the interface, including the editor and project windows
- Managing project builds and dependencies
- Creating new Java classes
- Refactoring code
- Using templates
- Using breakpoints and watch expressions
- Updating apps with Instant Run
- Using Git for version control
Skill Level Beginner
Q: This course was updated on 04/27/2017. What changed?
A: New videos were added that highlight the new features introduced in Android Studio 2.3. In addition, the following topic was updated: update apps with Instant Run.