Android Testing Options

Overview

Automated Testing is an important topic that helps us ensure quality when building Android apps. There are many different testing tools and frameworks we can use while developing Android apps. This guide will take a look at some of the more popular approaches available. For someone first starting with testing, we recommend looking at Robolectric for unit testing, Espresso for UI testing, Assertj-Android for better validation support, and Mockito for mocking.

Helpful external testing resources include:

Review the sections below for further resources and guides.

Unit Testing

Unit testing is about testing a particular component (i.e. an activity or model object) in isolation of other components. In Android, unit tests do not require a device or emulator. These tests are typically placed in the app/src/test/java folder.

  • Robolectric - Popular Android unit test framework that allows faster test execution by running tests on the JVM (no device or emulator needed).
  • JUnit - Popular Java unit test framework. Most of the Android test frameworks are built on top of JUnit.

Instrumentation Testing

Android Instrumentation is a set of "hooks" into the Android system that allow you to control the lifecycle of Android components (i.e. drive the activity lifecycle yourself instead of having these driven by the system). These tests require an actual device or emulator to run and are typically placed in the app/src/androidTest/java folder.

  • Espresso - Extensible Android UI Test Framework provided by Google that handles test synchronization very well.
  • UIAutomator - Android UI Test Framework provided by Google for testing across multiple apps at the same time.
  • Google Android Testing - This is the testing framework included as part of the platform.
  • Robotium - Third party Android UI Test Framework (comparison with Espresso)
  • Selendroid - Selenium for Android

Richer Validation Support

Simple JUnit assertions leave a lot to be desired when working on Android. The following libraries and classes help fill this gap.

  • Assertj-Android - An extension of AssertJ (fluent assertions for Java) extended for Android (formerly known as FEST Android).
  • MoreAsserts - Extension of JUnit assertions to add assertions for sets, lists, etc.
  • ViewAsserts - Helper methods to validate view layout

Mocking Frameworks

Mocking out dependencies is a very powerful capability when writing tests. These frameworks provide rich mocking support on Android.

  • Mockito - Popular mocking framework for Java
  • EasyMock - Mocking framework that has record / replay support

Tools

A collection of useful tools.

  • Cloud Test Lab - Large bank of devices where you can submit your tests and have them run across different devices.
  • MonkeyRunner - API Toolkit that allows controlling the Android device from Python code.
  • Monkey - Program that runs on the device and performs random clicks, text input, etc.
  • Spoon - Run your tests across multiple devices at the same time and see aggregated reports / screenshots.
Fork me on GitHub