How to do TDD in Android ? Part 1 — Overview
Automation tests are important as it ensures quality while developing application.TDD enforces writing tests before writing implementation.
Refer this previous tutorial for more information about TDD.
Testing pyramid :
As we can see Pyramid below,
Unit tests cover large section of pyramid making solid base. Unit tests are easy to write.
Integration Testing ensures integration of modules is correct and cover second large piece of Pyramid.
Functional testing usually describes what the system does and occupies smallest amongst the three part.
There are many testing tools and frameworks that can be used while developing application. Some of them are provided & supported by Google and some of them are third party .
Types of testing in Android :
Unit tests mainly target smallest functionality (like method, class , component or small module) with isolation from other component.
Tools used for Unit Test
- JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
- Robolectric is popular Android unit test framework that allows faster test execution by running tests on the JVM (no device or emulator needed😊).
Instrumentation Test (UI) are tests that mock User Interaction like clicking button , Typing text in EditText. Android Instrumentation is a set of “hooks” into the Android system that allows 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 .
Tools used for Instrumentation Test
- Espresso — 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.
- Robotium — Third party Android UI Test Framework (Robotium vs Espresso)
- Selendroid — Selenium for Android
Package Structure for Testing :-
In our project, two folders host our tests which are test and androidTest.
Test :Unit tests are hosted in this folder. These tests run on JVM and do not requires Android device or emulator. This type of test have no access to any android framework specific component like Context.
androidTest:- All Instrumentation (Espresso) tests are hosted in this folder.These tests need physical Android device or emulator in order to run.
In next post, we will dive deep into what architecture to follow & implementation for unit testing .
Meanwhile , would like to hear suggestions & inputs on this.
Cheers 🍻 !!!.