Android instrumentation is a set of control methods to control an Android component independently of its normal life cycle. They also control how Android loads applications.
android 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.
Normally, an Android component runs in a lifecycle determined by the system. For example, an Activity object's lifecycle starts when the Activity is activated by an Intent. The object's
onCreate()
method is called, followed by onResume()
. When the user starts another application, the onPause()
method is called. If the Activity code calls the finish()
method, the onDestroy()
method is called. The Android framework API does not provide a way for your code to invoke these callback methods directly, but you can do so using instrumentation. Also, the system runs all the components of an application into the same process. You can allow some components, such as content providers, to run in a separate process, but you can't force an application to run in the same process as another application that is already running.
With Android instrumentation, though, you can invoke callback methods in your test code. This allows you to run through the lifecycle of a component step by step, as if you were debugging the component. The following test code snippet demonstrates how to use this to test that an Activity saves and restores its state:
// Start the main activity of the application under test mActivity = getActivity(); // Get a handle to the Activity object's main UI widget, a Spinner mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01); // Set the Spinner to a known position mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION); // Stop the activity - The onDestroy() method should save the state of the Spinner mActivity.finish(); // Re-start the Activity - the onResume() method should restore the state of the Spinner mActivity = getActivity(); // Get the Spinner's current position int currentPosition = mActivity.getSpinnerPosition(); // Assert that the current position is the same as the starting position assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);
II. Test Case Classes
1.AndroidTestCase:
It extend bothTestCase
andAssert
. It provides the JUnit-standardsetUp()
andtearDown()
methods, as well as well as all of JUnit's Assert methods. In addition, it provides methods for testing permissions, and a method that guards against memory leaks by clearing out certain class references.
This test case provides a framework in which you can test Application classes in a controlled environment.
Class Overview: Extend this if you need to access Resources or other things that depend on Activity Context.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/test/AndroidTestCase.java
2.ApplicationTestCase
The test case can be useful in verifying that the <application> element in the manifest file is correctly set up. Note, however, that this test case does not allow you to control testing of the components within your application package.
3.InstrumentationTestCase
for testing some part of the activity in isolation
4.MockObjectClassMock Context class implements all methods in a non-functional way and throw UnsopportedOperationException.
This can be used to inject other dependencies, mocks, or monitors into the classes under testing. A fine control can be obtained extending this class.
Extend this class to provide your desired behaviour overriding the correspondent methods.
This document can be read in Google Docs : https://docs.google.com/View?id=ddwc44gs_21737cgtvfj
full details can be read at: http://developer.android.com/guide/topics/testing/testing_android.html
No comments:
Post a Comment