Android Test Automation with Espresso Testing Framework

Ahmet Koçu
Mobile Application Test Automation
4 min readMar 21, 2017

If you’re an Android developer, you’re probably familiar with Google’s Espresso test automation framework. As an open-source tool, it’s very easy for developers to use and extend within their working environment.

Firstly, we discuss about the importance of test automation in application development cycle. Development with manual way;

  • develop a feature
  • deploy to device
  • test manually
  • fix of wrong implementation
  • deploy to device and so on…

So how do you automatize such an inelegant mobile development process? How can you reduce your manual testing effort? Well, you can start doing what every software developer should do: Start writing tests.

What is the Espresso Framework

Testing user interactions within a single app helps to ensure that users do not encounter unexpected results or have a poor experience when interacting with your app. You should get into the habit of creating user interface (UI) tests if you need to verify that the UI of your app is functioning correctly. (Android Developer)

Espresso is targeted at developers, who believe that automated testing is an integral part of the development lifecycle. While it can be used for black-box testing, Espresso’s full power is unlocked by those who are familiar with the codebase under test. (Android Testing Support Library)

As quoted above, this framework is used to simulate the user input on the UI. With the use of the Espresso Framework you can perform manipulations on the UI and after assert the correct outcome of your input with a JUnit 4 Syntax. The Espresso Framework can be categorized between back-box and white box testing, commonly called as gray-box testing framework. With some modification in test code, it can be used for back-box tests.

How to setup the Espresso Framework

Firstly, On virtual or real device, it should be turn off system animations, because Espresso can not wait until the animation is finished.

On your device, under Settings->Developer options disable the following 3 settings:

  • Window animation scale
  • Transition animation scale
  • Animator duration scale

New versions of Android Studio will add test frameworks and configurations. If you have not test framework, you should add following libraries and configurations;

  • Make sure you have installed the latest Android Support Repository under Extras (see instructions).
  • Open your app’s build.gradle file. 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’

  • All other espresso framework libs:

// Android JUnit Runner
androidTestCompile ‘com.android.support.test:runner:0.5’

// JUnit4 Rules
androidTestCompile ‘com.android.support.test:rules:0.5’

// Espresso core
androidTestCompile ‘com.android.support.test.espresso:espresso-core:2.2.2’
// Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
androidTestCompile ‘com.android.support.test.espresso:espresso-contrib:2.2.2’
// Espresso-web for WebView support
androidTestCompile ‘com.android.support.test.espresso:espresso-web:2.2.2’
// Espresso-idling-resource for synchronization with background jobs
androidTestCompile ‘com.android.support.test.espresso:espresso-idling-resource:2.2.2’
// Espresso-intents for validation and stubbing of Intents
androidTestCompile ‘com.android.support.test.espresso:espresso-intents:2.2.2’

// UiAutomator
androidTestCompile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.2’

  • Add to the same build.gradle file the following line in android.defaultConfig:

testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”

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

It can be run multiple ways;

In android studio:

Create a test configuration

  • Open Run menu -> Edit Configurations
  • Add a new Android Tests configuration
  • Choose a module
  • Add a specific instrumentation runner: android.support.test.runner.AndroidJUnitRunner

From command-line via Gradle

./gradlew connectedAndroidTest

From command-line via adb instrument

Build your app and test apks and install both apks to test device, after that execute this command:

adb shell am instrument -w com.x.test/android.support.test.runner.AndroidJUnitRunner

More details about instrument

RobotQA Cloud Real Device Test Automation Solution

You can automate your application build process with CI tools, and integrate with our testing solution RobotQA.

RobotQA has more than 350 real devices and can run common test framework scripts. Espresso is our highly supported Android testing framework. You can upload application and test file to our service via web UI or RestApi, schedule a test run and get detailed report log files, video, screenshots, performance data.

As well as, you can use our record and play tool, record your test cases and manage it on cloud with collaboration.

Also, RobotQA can be integrated with your development lifecycle. Your test issues automatically transfer to your source control system.

--

--