Getting Started with Android Testing – Chapter 3: Android Testing Techniques
Nowadays, Android becomes the most popular mobile device’s OS over the world. Since 2007, there’re many Android OS versions were released. When developed an Android application, developers need to test his app on every version Android to make sure that it runs correctly. In this case, the number of test cases must be executed could be huge. It costs much effort to conduct test execution if do them manually. That’s why we need Automated Testing. One of the advanced features of Android Testing is automated testing techniques. With automated testing, you can:
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
This chapter introduces some testing frameworks, which are commonly used to conduct Android Automated Testing
Android Testing Framework
Robotium Testing framework
Roboelectric Testing framework
Android testing frameworkOverview One of the standard testing frameworks for Android application is Android testing framework. It is a powerful but easy-to-use testing framework that is well integrated with the Android SDK tools. Figure 8 summarizes the Android testing framework.
See in Figure 8:
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:
Test tools: A SDK tools for building and test. They are integrated into Eclipse IDE or run from command line
MonkeyRunner: A tool provides APIs for writing program which controls an Android device or emulator outside of Android code
Test package likes Android application, are organized into test projects. This package follows naming convention. Etc if the application under test has a package name of “com.mydomain.myapp” → Test package should be “com.mydomain.myapp.test” .Tests package includes two objects as below:
Test case classes include test methods to executed on target application
Mock objects includes mock data that will be used as sample input for test cases Android Test Case Classes
See in Figure 9:
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 or others like Activity Context.
ApplicationTestCase verifies the Application classes in a controlled environment.
InstrumentationTestCase verifies a particular feature or behavior of target application. Etc, verify UI output of application.
ActivityTestCase is base class supports testing the Application Activities.
ProviderTestCase is class for testing single ContentProvider.
ServiceTestCase is used to test Service classes in testing environment, also support Service’s life cycle.
SingeLauchActivityTestCase is used to test single Activity with an InstrumentationTestCase.
ActivityUnitTestCase is used to test single activity isolated.
ActivityInstrumentationTestCase2 extends the JUnit TestCase class. It connects you to target application with instrumentation. With this class, you can access to application’s GUI component and send UI event (keystroke or touch event) to the UI. Figure 10 is an example of ActivityInstrumentationTestCase. It verifies the UI operation of Calculator application, checks the correctness of the UI outputs.
Robotium testing frameworkOverview Standard Android testing framework has some limitation such as:
Unable to handle multiple activities
Test execution performance is slow
Test cases is complex, hard to implement
Robotium framework is another choice to conduct testing on Android application
Robotium is open source framework and considered as an extension of Android test framework. Using Robotium, developer can create robust automatic GUI test cases for Android applications. Moreover, developer can write function, system and acceptance test scenarios, spanning multiple Android activities. The advance features of Robotium are shown in Figure 11.
Robotium Test Case Classes Robotium uses set of classes for testing which is com.jayway.android.robotium.solo. This class supports test cases that span over multiple activities. Solo is integrated with the ActivityInstrumentationTestCase2 as shown in Figure 12.
The tester can write test cases without knowledge of application design (black box testing) by using Robotium test case classes. It is an outstanding feature compare to Android test case classes. To use Robotium in your Android test project, you need follow these steps as show in Figure 13
Detail of installing and using Robotium will be described in Chapter 4, START FIRST ANDROID TESTING Roboelectric testing frameworkOverview Testing using Android Testing Framework without device or emulator is difficult. Building and running test is slow and take much the development’s effort. To fix this problem, there’s another choice is using roboelectric testing framework. Roboelectric framework allows you to run Android tests directly on JVM without the need for a device or an emulator. The advance features of Roboelectric are shown in Figure 15.
Roboelectric Test Case Classes
As shown in Figure 15, Roboelectric do following actions:
Register and create a Shadow class
Intercept the loadjava assistroid class
Uses java assist to override the method bodies of Android class
Bind Shadow object to Android class
These allow 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: