UNIT Testing - Android 





                                                                 

                                                                                                                             Sagar C A 

      Android Team- CoE


Testing 

A system under test (SUT) is assessed using many methodologies and techniques to provide information about the quality of the system and to know whether the product is acceptable by the end users.


Primary purpose  is to detect and fix bugs/defects "early " in the software development life cycle.

Process of validating and verifying the SUT :

  • Meets the requirements

  • Works as expected

  • Satisfies the needs of project stakeholders


Test Levels


Tests are frequently grouped by where they are added in the software development cycle or by the level of specificity.


The main levels during the development process are:

  • Unit
  • Integration
  • System
  • Acceptance






Unit Test


  • Piece of code written by a developer that executes a specific functionality in the code which is tested.
                                 

  • A unit test targets a small unit of code, e.g. a method or a class.


  • The percentage of code which is tested by unit tests is typically called test coverage.



 

Android testing framework

The Android testing framework is an integral part of the development environment, provides an architecture and powerful tools that help you test every aspect of your application at every level from unit to framework.

The testing framework has these key features:

  • Android test suites are based on JUnit. Can use plain JUnit to test a class that doesn't call the Android API, or Android's JUnit extensions to test Android components.

  • The Android JUnit extensions provide component-specific test case classes. These classes provide helper methods for creating mock objects and methods that help you control the life-cycle of a component.


Contd ..

  • Test suites are contained in test packages that are similar to main application packages, so not required to learn a new set of tools or techniques for designing and building tests.

  • The SDK tools for building and tests are available in Eclipse with ADT. These tools get information from the project of the application under test and use this information to automatically create the build files, manifest file, and directory structure for the test package.

Contd ..

The Android testing framework

Test Structure

  • Android's build and test tools assume that test projects are organized into a standard structure of tests, test case classes, test packages, and test projects.

  • Android testing is based on JUnit. In general, a JUnit test is a method whose statements test a part of the application under test. Test methods are organized into classes called test cases (or test suites). Each test is an isolated test of an individual module in the application under test. Each class is a container for related test methods, it provides helper methods as well.

  • Also uses the SDK's build tools to build one or more test source files into class files in an Android test package, test tools to load the test package and the application under test, and the tools then execute an Android-specific test runner.

Test Project

Tests, like Android applications, are organized into projects.

A test project is a directory or Eclipse project in which you create the source code, manifest file, and other files for a test package. The tools create the directories you use for source code and resources and the manifest file for the test package.

We must always use Android tools to create a test project. Among other benefits, the tools:

  • Automatically set up your test package to use InstrumentationTestRunner as the test case runner. You must use InstrumentationTestRunner (or a subclass) to run JUnit tests.
  • Create an appropriate name for the test package. If the application under test has a package name of com.mydomain.myapp, then the Android tools set the test package name to com.mydomain.myapp.test. This helps you identify their relationship, while preventing conflicts within the system.

CONTD ..

  • Automatically create the proper build files, manifest file, and directory structure for the test project. This helps to build the test package without having to modify build files and sets up the linkage between the test package and the application under test.

TEsting API - JUnit

Android testing API is based on the JUnit API and extended with a instrumentation framework and Android-specific testing classes.

  • Can use the JUnit TestCase class to do unit testing on a class that doesn't call Android APIs. TestCase is also the base class for AndroidTestCase, which you can use to test Android-dependent objects. Besides providing the JUnit framework, AndroidTestCase offers Android-specific setup, teardown, and helper methods.

  •   Can use the JUnit Assert class to display test results. The assert methods compare values we expect from a test to the actual results and throw an exception if the comparison fails. Android also provides a class of assertions that extend the possible types of comparisons which are described in more detail in the section Assertion classes

Instrumentation

  • Android instrumentation is a set of control methods or "hooks" in the Android system. These hooks control an Android component independently of its normal life-cycle and they also control how Android loads applications.

  • Normally, an Android component runs in a life-cycle determined by the system.The Android framework API does not provide a way for your code to invoke callback methods (onCreate(),onResume(),onPause(), finish() ,onDestroy()) directly, but can be done using instrumentation. The key method used is getActivity(), which is a part of the instrumentation API. The Activity under test is not started until you call this method. We can set up the test fixture in advance, and then call this method to start the Activity.

  • Also, instrumentation can load both a test package and the application under test into the same process. Since the application components and their tests are in the same process, the tests can invoke methods in the components, and modify and examine fields in the components.

Component-specific test cases

  • A key feature of the Android testing framework is its component-specific test case classes which address specific component testing needs with methods for fixture setup and teardown and component life-cycle control. These classes are described in the component-specific testing topics:
  • Android does not provide a separate test case class for BroadcastReceiver. Instead, test a BroadcastReceiver by testing the component that sends it Intent objects, to verify that the BroadcastReceiver responds correctly.

Activity Testing

The test application contains methods that perform the following tests:

  • Initial conditions test
:
Tests that the application under test initializes correctly. This is also a unit test of the application's onCreate() method. It also provides a confidence measure for subsequent tests.
  • UI test :

Tests that the main UI operation works correctly. It demonstrates the instrumentation features available in activity testing. Also shows that you can automate UI tests by sending key events from the test application to the main application.
  • State management tests :

Test the application's code for saving state. It demonstrates the instrumentation features of the test runner, which are available for testing any component.

Testcase Class

  • Test setup :
This use of the JUnit setUp() method demonstrates some of the tasks that can be performed before running each Android test.
  • Testing initial conditions:  

This demonstrates that with Android instrumentation we can look at the application under test before the main activity starts. The test checks that the application's important objects have been initialized. If the test fails, it ensures that  subsequent/other tests against the application are unreliable, since the application was running in an incorrect state.
Note: The purpose of testing initial conditions is not the same as using setUp(). The JUnit setUp() runs once before each test method, and its purpose is to create a clean test environment. The initial conditions test runs once, and its purpose is to verify that the application under test is ready to be tested.



contd ..

  • Testing the UI:

This test shows how to control the main application's UI with instrumentation, a powerful automation feature of Android testing.

  • Testing state management:

This test shows some techniques for testing how well the application maintains state in the Android environment. To provide a satisfactory UX, the application must never lose its current state, even if it's interrupted by a phone call or destroyed because of memory constraints.
The Android activity lifecycle provides ways to maintain state. The test shows the techniques for verifying that they work as expected.

CONTD ..

  • Android tests are contained in a special type of Android application that contains one or more test class definitions. Each of these contains one or more test methods that do the actual tests.  So we first add a test case class and then add tests to it.


  • Also, we first choose an Android test case class to extend. It needs to be chosen from the base test case classes according to the Android component you are testing and the types of tests you are doing.

  • For activity test case classes, ActivityInstrumentationTestCase2 is used as the base class. It offers convenience methods for interacting directly with the UI of the application under test.

TESts run and results

  • Run As > Android JUnit Test:
  • The test application starts. You see a new tab for the JUnit view, next to the Package Explorer tab
  • At the conclusion of a successful test run, below is view's appearance :

CONsole



References

http://developer.android.com/tools/testing/testing_android.html#TestAPI


http://developer.android.com/training/activity-testing/activity-unit-testing.html#testcase


http://developer.android.com/tools/testing/activity_test.html#RunTests


http://developer.android.com/training/activity-testing/activity-basic-testing.html


UNIT Testing - Android

By Torry Harris Business Solutions