-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Android Testing Options
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.
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.
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
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 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
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.
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.