13.6 Android Testing Fundamentals

Ladies and gentlemen!! HelloJ! Today we are going to start one of the most set fire topics of software development and that is “Testing!!” As we all know testing is one of the bright career aspects for young generation. Although this is not a tutorial for testing but it is very important to know the testing procedure as we are dealing with software projects. Let us not waste time and start studying.

13.6.1 Introduction

Bug is a generic term given to flaws, mistakes, or errors and they are inevitable. Early detection of bugs is the best solution to save your pocket. Bugs delays shipment and costs you money at the same time. Test Driven development is an agile component of software development process. It helps us confront the bugs earlier in the development process. The best known reason to write software tests for your development project is that early bug detection saves a huge amount of project resources and reduces software maintenance costs. Now writing the tests also gives us a better understanding of the requirements and the problem to be solved. If we talk in general then we have an open-source software toolkit for measuring and reporting Java code coverage. It covers following types:

  • Basic block
  • Line
  • Method
  • Class

EMMA is supported by Android framework. We can easily build an EMMA instrumented version of Android. But you can only use it for JUnit tests. Tests should be automated. There are some specific areas of Android applications which should be considered. Let us have a quick glance on these areas:

  • Activity lifecycle events: We should test our activities that it is capable of handling lifecycle events properly: We should check whether activity should save its state during onPause() or onDestroy() events. We should also check whether it is capable of restoring it in onCreate(Bundle savedInstanceState). We should be able to reproduce and test these conditions and verify that the state was correctly saved and restored.
  • Database and file system operations: These operations should be tested to ensure that they are handled appropriately. These operations need to be tested in isolation at the lower system level. At higher level, these should be tested through ContentProviders and from application also.
  • Physical properties of devices: We need to be sure that all of the different devices are supported or at least should allow detecting the situation and taking appropriate measures before delivering our application. Some of the characteristics which are of importance in terms of testing are as follows:
    • Screen densities
    • Screen resolution
    • Screen sizes
    • Network capabilities
    • Availability of sensors
    • GPS
    • External storage
    • Input devices along with keyboard

13.6.2 Overview on Types of Testing

A test should verify a condition and return the result of this evaluation as a single Boolean value which in turn will indicate success or failure. Different types of tests are possible according to the object being tested. Some of them are as follows:

  • Unit Tests: Unit tests are software tests written by programmers, for programmers using programming language. These tests should be able to isolate the component under test and be able to test it in a repeatable way. Mock objects and unit tests are usually placed together. Mock objects can be used to monitor interactions, isolate unit from dependencies, etc.
  • Text fixture: It is defined as a baseline which is used to run tests. This is shared by all test cases.
  • Test Preconditions: It is recommended to create tests for preconditions.
  • Actual tests: The public void method which starts with test are to be considered as a test.
  • Mock objects: Mock objects are mimic objects. These are used instead of calling actual objects.
  • UI tests: The main thread is allowed to alter the UI in Android.
  • Integration tests: These tests are designed to test the way individual components work collectively. After testing the modules independently, they are tested collectively. They require activity lifecycle. This is provided by ActivityManager. It also needs access to resources, databases and file systems.
  • Functional or acceptance tests: These are high level tests. These tests test the completeness and correctness of a user requirement or feature. Usually, it targets the quality assurance.
  • Performance Tests: These tests deal with the performance metrics of application. It is always better to test a change before and after implementing the change respectively.
  • System Tests: Complete system is tested. The way components work independently and as a whole. It include GUI tests, Performance tests, Installation tests, etc.

13.6.3 Testing and Android

Android testing framework involves following features:

  • The test suites are based on JUnit. A JUnit to test a class and it does not involve any call to Android API. However, you can use Android’s JUnit extension to test components.
  • Android JUnit extension can provide component specific test case classes. Helper methods are provided by these classes
  • Like application packages, test suites are contained in Test package
  • The tools which are available for building and testing are available with Eclipse and other IDE’s via command line. They get information from the project of the application under test. They use this information to automatically create the build files, manifest file, and directory structure for the test package.
  • An API for testing devices with Python programs called monkeyrunner is provided by SDK.

Framework for android testing is summarized in the following diagram:

Figure Android Testing Framework

Instrumentation framework controls the application under test. It permits the injection of mock components required by the application to execute. All the interaction with near by environment can be controlled easily. Targets to be tested should be taken care of.

Congratulations ladies and gentlemenJ!! We are concluding the introduction with this. See you in the next section. Till then keep practicing. Happy App developingJ!!