Blog about nerd stuff, programming and related tools

Android UI Tests

Published / by glethien / Leave a Comment

Testing your app before release is a very important part! You don’t want to release a buggy and crashing app to the world! So the question is how to improve the app and find the bugs? For this you can use memory profiler to find memory leaks, but most important you want to test you app with near real-world-situations. Doing this test by hand is very inefficient and time consuming.

Luckily Google have the solution right in your Android SDK: Espresso! Not the coffee but the testing framework. With some short changes in your gradle build file you can integrate Espresso into your project! Just add the following lines to you gradle file.

dependencies {
    // Other dependencies ...
    androidTestCompile ''

And you are ready to go! You may have notices that besides you normal package in AndroidStudio there is the same package again but with “androidTesting”. In this package just create an empty class and annotate it with

public class MyFirstTest {

This will tell expresso that this class is a TestClass which contains serveral other tests (at least you should implement one test).

Before you write the first test you need to add a rule to you test. This rule will be the activity in which your test will start!

public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

Now it is time for your first test. Add a new method and annotate it using @Test

    publicvoid changeTextTest(){
        // Type text and then press the button.
                .perform(typeText("Hello World"), closeSoftKeyboard());

        // Check that the text was changed.
                .check(matches(withText("Hello World")));

This test is a little bit stupid but I think it will demonstrate how Espresso works. You have a lot of static methods which you can use to implement the test logic. Normally the test should be nearly human understandable. Look a the first line in the test. It tells Espresso to take the view with the Id and perform something. In this case typeText(“Hello World”) and then close the keyboard. Pretty straight forward.

Next line will be much more easier! With the view, which has the ID changeTextBt perform the click. Simple as that.

But keep in mind: you can only check for UI elements which are currently visible. As soon as the matching fails, the test aborts and you will get a failure. This contains that there is no view with the desired id or the texts don’t match. So when you write the UI tests, you should consider that.

Espresso has some backdraws too. You can not call methods from the activity which are not bound to buttons, or views in general. You do not have other information as those the user can see! There are ways to get those but, personally I think, that if you need those, your app or your test has a bad architecture.

For all Espresso checks you can see the cheat sheet here.


Leave a Reply

Your email address will not be published. Required fields are marked *