Android is the largest operating system in the world. At the same time, Android is fragmented. there are tons of devices and Android versions that your app must be compatible with.
It doesn’t matter how much time you invest in design and implementation , mistakes are inevitable, and bugs will appear.
In this tutorial, you will learn-
- Why Android Testing?
- Android Testing Strategy
- Unit tests
- Integration tests
- Operational tests
- System tests
- Automated ANDROID TESTING
- Android testing framework
- Robolectric testing framework
- Myths of Android Testing
- Best practices in Android Testing
A correct android testing strategy should include the following
- Unit Test
- Integration Test
- Operational Test
- System Test
Unit Tests include sets of one or more programs which are designed to verify an atomic unit of source code, such as a method or a class.
Android platform comes pre-integrated Junit 3.0 framework. It’s open source framework for automating Unit Testing. Android Testing Framework is powerful tool for developer to write the effective unit test program.
The integration of Android and JUnit framework
An addition to Unit Testing is User Interface (UI) tests. These tests relate to UI components of your target application. UI tests ensure that your application return the correct UI output in response to sequence of user actions on device.
Common user UI actions on application
In Integration Testing, all unit tested modules, are combined and verified. In Android, integration tests often involve checking integration withAndroid components such as Service testing, Activity testing, Content Provider testing, etc
Types of integration test on Android
There’s many testing frameworks are used to conduct integration test for Android such as Troyd, Robolectric, Robotium.
- Operational are also called Functional Tests or Acceptation Tests. They are high level tests designed to check the completeness and correctness of application.
- In Android, FitNesse is open-source framework that makes it easy to conduct operational tests for target application.
In System Testing the system is tested as a whole and the interaction between the components, software and hardware is checked.
In Android, System Testing normally includes
- GUI tests
- Usability tests
- Performance tests
- Stress tests
In the above list, Performance Testing is given more focus. You can use tools like Traceview to conduct performance test on Android .This tool can help you debug your application and profile its performance.
As android is fragmented, testing on multitude of devices is necessary. But this will also cost you money. Automated Android Testing can help reduce costs
Benefits of automated android testing
- Reduce time for executing test cases
- Increase productivity of your development process
- Early bug detection, save cost on software maintenance
- Quickly found and fix the bugs on implementation
- Ensure the quality of software
We will study the following 2 frameworks
- Android Testing framework
- Robolectric Testing framework
One of the standard testing frameworks for Android application is Android testing framework. It is a powerful and easy-to-use testing framework that is well integrated with the Android SDK tools.
Android testing framework Architecture
- Application package is your target application which needs to be tested
- InstrumentationTestRunner is the Test Case runner that executes test case on target application. It includes:
2a) Test tools: A SDK tools for building test. They are integrated in Eclipse IDE or run as command line.
2b) MonkeyRunner: A tool that provides APIs for writing program which control an Android device or emulator outside of Android code.
- Test package are organized into test projects. This package follows naming convention. If the application under test has a package name of “com.mydomain.myapp” than Test package should be “com.mydomain.myapp.test” .Test package includes 2 objects as below:
3a) Test case classes:include test methods to executed on target application.
3b) Mock objects : includes mock data that will be used as sample input for test cases.
Android Test Case Classes
AndroidTestCase class diagram
- TestCase includes JUnit methods to run JUnit test
- TestSuite is used to run set of test cases
- InstrumentationTestSuite is a TestSuite that injects Instrumentation into InstrumentationTestCase before running them.
- InstrumentationTestRunner is the test case runner that execute test case on target application.
- AndroidTestCase extends JUnit TestCase. It contains methods for accessing resources like Activity Context.
- ApplicationTestCase verifies the Application classes in a controlled environment.
- InstrumentationTestCase verifies a particular feature or behavior of target application , for e.g., verify UI output of application.
- ActivityTestCase is base class that supports testing the Application Activities.
- ProviderTestCase is class for testing single ContentProvider.
- ServiceTestCase is used to test Service classes in testing environment. It also supports Service’s life cycle.
- SingeLauchActivityTestCase is used to test single Activity with an InstrumentationTestCase.
- ActivityUnitTestCase <Activity> is used to test single isolated activity.
- ActivityInstrumentationTestCase2<Activity> extends the JUnit TestCase class. It connects you to target application with instrumentation. With this class, you can access application’s GUI component and send UI event (keystroke or touch event) to the UI.
Below is an example of ActivityInstrumentationTestCase. It verifies the UI operation of Calculator application, check the correctness of the UI outputs.
ActivityInstrumentationTestCase2 testing example
Testing using Android Testing framework with device or emulator is difficult. Building and running test is slow and take much development effort. To fix this problem, there’s another choice – Robolectric testing framework.
Robolectric framework allows you to run Android tests directly on JVM without the need for a device or an emulator.
Advance features of Robolectric
Robolectric Test Case Classes
Operation of Robolectric
- As shown above, Robolectric can perform following actions:
- Register and create a Shadow class
- Intercept the loading of Android class
- Uses javaassist to override the method bodies of Android class
- Bind Shadow object to Android class
- This allows the code under test to execute without Android environment.
Others testing framework
Besides testing frameworks which were mentioned above, there are many other testing frameworks such as:
- Android Junit Report, a custom instrumentation test runner for Android that generates XML reports for integration with other tools.
Many enterprises develop android Testing strategies that are based on common misconceptions. This section examines a few popular myths and realities of Android testing.
Myth #1:All Android devices are the same… test on emulators is enough
Let’s start with a simple example. An application works perfectly on emulator s but on some real devices, it crashes during execution
Application crashes during execution on real device
Emulators are not sufficient for your mobile testing. You must test your app on real devices.
Myth #2:Testing on some common devices is enough
- On different devices, your application looks different because different devices have different hardware, screen sizes, memory etc. You must test your application on different devices, OS versions, carrier networks and locations.
Myth#3:Exploratory testing just before launch is enough
- Generally in all testing, we design the test cases then execute them. But in Exploratory testing, test design and execution all will be done together.
- In exploratory testing, there’s no plan and no preparation, then tester would do tests that he wants to do. Some functions will be tested repeatedly, while some functions will not be tested altogether.
Myth#4:If there some bugs in application, users will understand
- If application doesn’t work and has bugs, users uninstall your app
- Quality issues are the first reason for bad review in Google Play. It affects to your reputation and you lose customer’s trust.
Therefore its essential to have a proper android testing strategy in place
- Application developers should create the test cases at the same time when they are writing the code
- All test cases should be stored in version control-together with source code
- Use continuous integration and run tests every time the code is changed
- Avoid using emulators and rooted devices