Updated about 4 hours ago | GitHub

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, Truth 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.
  • Instrumented tests - Google’s official documentation for tests that run on a device or emulator.
  • Robotium - Third party Android UI Test Framework (comparison with Espresso). The project is no longer actively maintained — the repository’s master branch has not changed since September 2016.
  • Selendroid - Selenium for Android. The project is no longer actively maintained — the repository’s master branch has not changed since November 2018.

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.

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.

  • Firebase Test Lab (formerly Cloud Test Lab) - Large bank of devices where you can submit your tests and have them run across different devices.
  • MonkeyRunner - Legacy API toolkit that allows controlling the Android device from Python code. The tool’s documentation cautions that “The monkeyrunner API is unmaintained” and recommends the UI Automator testing framework instead.
  • 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. The project is no longer maintained (its GitHub repository is archived).