The user interface (UI) is an important part of the functionality of Android mobile apps, so you should be able to perform extensive UI tests. Google’s Mona El Mahdy has published has published on the Google Software Testing blog a post that presents four strategies to test Android UI, aiming to create UI tests that are both fast, reliable and easy to debug.
UI testing is defined as “ensurîng that your application returns the correct UI output in response to a sequence of user actions on a device.” Mona El Mahdy starts with a reminder of the importance of doing as much unit testing as possible. To test the Android UI, she recommends using Espresso that she defines as a “great framework for running UI actions and verifications in the same process”.
Strategy 1: Using an End-To-End Test as a UI Test
The problem with End-To-End (E2E) tests is that they require that everything (backend servers, networks, etc.) works fine. UI tests can work with just a simulation of these conditions. The next strategies aim at fixing the problems of E2E tests.
Strategy 2: Hermetic UI Testing using Fake Servers
This approach limits the test size, but you need to maintain a separate fake server as well as your test. It is not easy to debug as you have to inspect the test and the local server.
Strategy 3: Dependency Injection Design for Apps
Dependency injection is usde in the Android application to swap real module implementations with fake ones. To achieve this goal, you can use Dagger, an open source dependency injector for Android and Java.
Strategy 4: Building Apps into Smaller Libraries
In this strategy, you build your Android application into small components/libraries, each of them having its own UI resources and user dependency management. When you have small components with dependency injection support, you can build a test applications for each component.
The conclusions of the post are:
“1. Don’t write E2E tests instead of UI tests. Instead write unit tests and integration tests beside the UI tests.
2. Hermetic tests are the way to go.
3. Use dependency injection while designing your app.
4. Build your application into small libraries/modules, and test each one in isolation. You can then have a few integration tests to verify integration between components is correct.
5. Componentized UI tests have proven to be much faster than E2E and 99%+ stable. Fast and stable tests have proven to drastically improve developer productivity.”
Read the complete blog post on http://googletesting.blogspot.ch/2015/03/android-ui-automated-testing.html