Monday, April 20, 2009

Android Instrumentation

Android Instrumentation

To test an android application, Android provides a class called Instrumentation where the programmers can derive this base class and write their own test cases. Instrumentation testing allows to verify a particular feature or behavior with an automated JUnit TestCase. We can perform the following functionalities with the class derived from Instrumentation.

· launch activities and providers within an application

· send key events automatically

· make assertions about various UI elements e.g. if a particular UI component is focussed or if the Flipper is flipping etc.,.

Once the test cases are ready, it can be executed from command line or thorough Emulator wherein a separate instrumentation application is built and installed. This Instrumentation Application can be found under the ‘Dev Tools’.


How to write an Instrumentation class?

1. An test application can be derived from an Instrumentation base class or can be derived from any of the following Instrumentation derived classes.









2. Each method that contains the testcases should start with ‘test’

3. Methods to be called to assert an test are

· assertNull()

· assertTrue()

· assertEquals()

· assertNotSame() ………….. etc

Much more methods are found to assert the test cases.

4. Once the test class is ready, the application should be informed about the Instrumentation class. Therefore the AndroidManifest.xml file has to be included with the instrumentation tag. This is apart from the application so, is has to added outside <application> …. </application>. And also indicate the application to use the Instrumentation library.

5. Example,

6. For example, Consider an example application whose Test class is derived from Instrumentation class.

How to run an Instrumentation class?
  1. Build the application. The instrumentation class also gets built. Then run the application. The ApplicationInstrumentation.apk and the application.apk both gets installed. ApplicationInstrumentation.apk can be found under ‘Dev Tools’ with the given label in AndroidManifest file. Once this is clicked, the application is tested automatically and the output is displayed in the LogCat.

  2. From the command promt also the application can be tested. Under the android installed drive, type the following command.

adb shell am instrumentation –w <your target package>/android.test.InstrumentationTestRunner

The test results of the instrumetaion are displayed in the command window itself.