This tutorial describes how to write instrumentation tests for your Android application. It covers the usage of Espresso to write UI tests and how to run these tests via Android studio.
Writing and maintaining a good test suite containing different types of tests requires a good amount of time and discipline on the part of the developer. However, the benefits that comes with doing this includes the following:
- Helps to ensure the correctness of the app features.
- Helps to implement new features with confidence that the new changes will not break existing working code.
- Facilitates building of app features in modules and iterations especially when tests are first written [TDD], then codes are written to validate the test.
Categories of Android unit tests
Local unit tests: These are tests that runs on the JVM. They do not require any native Android library to run. They are found in
app/src/test/javafolder in the Project perspective. Unit test should amount to about 60-70% of the test code base.
Integration test: Tests more than one individual module working together that implements a complete functional part of an app. Integration test are located in the
app/src/androidTest/javafolder and requires the Android system to run. Should amount to about 20% of the test code base.
Espresso is a testing framework contained in the Android Testing Support Library. It provides APIs to simulate user interactions and write functional UI tests.
Espresso tests are written based on what user might do while interacting with your app. Basically, you:
- Locate the desired UI element
- Interact with the UI element or check its state.