Added in API level 1
Deprecated in API level 24

InstrumentationTestRunner


open class InstrumentationTestRunner : Instrumentation, TestSuiteProvider

An Instrumentation that runs various types of junit.framework.TestCases against an Android package (application).

Typical Usage

  1. Write junit.framework.TestCases that perform unit, functional, or performance tests against the classes in your package. Typically these are subclassed from:
  2. Set the android:targetPackage attribute of the <instrumentation> element in the test package's manifest. You should set the attribute value to the package name of the target application under test.
  3. Run the instrumentation using "adb shell am instrument -w", with no optional arguments, to run all tests (except performance tests).
  4. Run the instrumentation using "adb shell am instrument -w", with the argument '-e func true' to run all functional tests. These are tests that derive from android.test.InstrumentationTestCase.
  5. Run the instrumentation using "adb shell am instrument -w", with the argument '-e unit true' to run all unit tests. These are tests that do notderive from android.test.InstrumentationTestCase (and are not performance tests).
  6. Run the instrumentation using "adb shell am instrument -w", with the argument '-e class' set to run an individual junit.framework.TestCase.

Running all tests: adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner

Running all small tests: adb shell am instrument -w -e size small com.android.foo/android.test.InstrumentationTestRunner

Running all medium tests: adb shell am instrument -w -e size medium com.android.foo/android.test.InstrumentationTestRunner

Running all large tests: adb shell am instrument -w -e size large com.android.foo/android.test.InstrumentationTestRunner

Filter test run to tests with given annotation: adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner

If used with other options, the resulting test run will contain the union of the two options. e.g. "-e size large -e annotation com.android.foo.MyAnnotation" will run only tests with both the LargeTest and "com.android.foo.MyAnnotation" annotations.

Filter test run to tests without given annotation: adb shell am instrument -w -e notAnnotation com.android.foo.MyAnnotation com.android.foo/android.test.InstrumentationTestRunner

Running a single testcase: adb shell am instrument -w -e class com.android.foo.FooTest com.android.foo/android.test.InstrumentationTestRunner

Running a single test: adb shell am instrument -w -e class com.android.foo.FooTest#testFoo com.android.foo/android.test.InstrumentationTestRunner

Running multiple tests: adb shell am instrument -w -e class com.android.foo.FooTest,com.android.foo.TooTest com.android.foo/android.test.InstrumentationTestRunner

Running all tests in a java package: adb shell am instrument -w -e package com.android.foo.subpkg com.android.foo/android.test.InstrumentationTestRunner

Including performance tests: adb shell am instrument -w -e perf true com.android.foo/android.test.InstrumentationTestRunner

To debug your tests, set a break point in your code and pass: -e debug true

To run in 'log only' mode -e log true This option will load and iterate through all test classes and methods, but will bypass actual test execution. Useful for quickly obtaining info on the tests to be executed by an instrumentation command.

To generate EMMA code coverage: -e coverage true Note: this requires an emma instrumented build. By default, the code coverage results file will be saved in a /data//coverage.ec file, unless overridden by coverageFile flag (see below)

To specify EMMA code coverage results file path: -e coverageFile /sdcard/myFile.ec
in addition to the other arguments.

Summary

Constants
static String

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the name of the current test class.

static String

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the name of the current test.

static String

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the sequence number of the current test.

static String

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the total number of tests that are being run.

static String

If included in the status bundle sent to an IInstrumentationWatcher, this key identifies a stack trace describing an error or failure.

static String

This value, if stored with key android.app.Instrumentation#REPORT_KEY_IDENTIFIER, identifies InstrumentationTestRunner as the source of the report.

static Int

The test completed with an error.

static Int

The test completed with a failure.

static Int

The test completed successfully.

static Int

The test is starting.

Inherited constants
String REPORT_KEY_IDENTIFIER

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the class that is writing the report. This can be used to provide more structured logging or reporting capabilities in the IInstrumentationWatcher.

String REPORT_KEY_STREAMRESULT

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies a string which can simply be printed to the output stream. Using these streams provides a "pretty printer" version of the status & final packets. Any bundles including this key should also include the complete set of raw key/value pairs, so that the instrumentation can also be launched, and results collected, by an automated system.

Public constructors

Public methods
open TestSuite!

Override this to define all of the tests to run in your package.

open Bundle!

Get the arguments passed to this instrumentation.

open ClassLoader!

Override this to provide access to the class loader of your package.

open TestSuite!

open Unit
onCreate(arguments: Bundle!)

open Unit

Protected methods
open Unit
addTestListener(listener: TestListener!)

Add a TestListener

open AndroidTestRunner!

Inherited functions
TestLooperManager! acquireLooperManager(looper: Looper!)

Takes control of the execution of messages on the specified looper until TestLooperManager.release is called.

Unit addMonitor(monitor: Instrumentation.ActivityMonitor!)

Add a new ActivityMonitor that will be checked whenever an activity is started. The monitor is added after any existing ones; the monitor will be hit only if none of the existing monitors can themselves handle the Intent.

Instrumentation.ActivityMonitor! addMonitor(filter: IntentFilter!, result: Instrumentation.ActivityResult!, block: Boolean)

A convenience wrapper for addMonitor(android.app.Instrumentation.ActivityMonitor) that creates an intent filter matching ActivityMonitor for you and returns it.

Instrumentation.ActivityMonitor! addMonitor(cls: String!, result: Instrumentation.ActivityResult!, block: Boolean)

A convenience wrapper for addMonitor(android.app.Instrumentation.ActivityMonitor) that creates a class matching ActivityMonitor for you and returns it.

Unit addResults(results: Bundle!)

Report some results in the middle of instrumentation execution. Later results (including those provided by finish) will be combined with android.os.Bundle#putAll.

Unit callActivityOnCreate(activity: Activity!, icicle: Bundle!)

Perform calling of an activity's android.app.Activity#onCreate method. The default implementation simply calls through to that method.

Unit callActivityOnCreate(activity: Activity!, icicle: Bundle!, persistentState: PersistableBundle!)

Perform calling of an activity's android.app.Activity#onCreate method. The default implementation simply calls through to that method.

Unit callActivityOnDestroy(activity: Activity!)

Unit callActivityOnNewIntent(activity: Activity!, intent: Intent!)

Perform calling of an activity's android.app.Activity#onNewIntent method. The default implementation simply calls through to that method.

Unit callActivityOnNewIntent(activity: Activity, intent: Intent, caller: ComponentCaller)

Same as callActivityOnNewIntent(android.app.Activity,android.content.Intent), but with an extra parameter for the ComponentCaller instance associated with the app that sent the intent.

Unit callActivityOnPause(activity: Activity!)

Perform calling of an activity's Activity.onPause method. The default implementation simply calls through to that method.

Unit callActivityOnPictureInPictureRequested(activity: Activity)

Perform calling of an activity's Activity.onPictureInPictureRequested method. The default implementation simply calls through to that method.

Unit callActivityOnPostCreate(activity: Activity, savedInstanceState: Bundle?)

Perform calling of an activity's android.app.Activity#onPostCreate method. The default implementation simply calls through to that method.

Unit callActivityOnPostCreate(activity: Activity, savedInstanceState: Bundle?, persistentState: PersistableBundle?)

Perform calling of an activity's android.app.Activity#onPostCreate method. The default implementation simply calls through to that method.

Unit callActivityOnRestart(activity: Activity!)

Perform calling of an activity's Activity.onRestart method. The default implementation simply calls through to that method.

Unit callActivityOnRestoreInstanceState(activity: Activity, savedInstanceState: Bundle)

Perform calling of an activity's android.app.Activity#onRestoreInstanceState method. The default implementation simply calls through to that method.

Unit callActivityOnRestoreInstanceState(activity: Activity, savedInstanceState: Bundle?, persistentState: PersistableBundle?)

Perform calling of an activity's android.app.Activity#onRestoreInstanceState method. The default implementation simply calls through to that method.

Unit callActivityOnResume(activity: Activity!)

Perform calling of an activity's Activity.onResume method. The default implementation simply calls through to that method.

Unit callActivityOnSaveInstanceState(activity: Activity, outState: Bundle)

Perform calling of an activity's android.app.Activity#onSaveInstanceState method. The default implementation simply calls through to that method.

Unit callActivityOnSaveInstanceState(activity: Activity, outState: Bundle, outPersistentState: PersistableBundle)

Perform calling of an activity's android.app.Activity#onSaveInstanceState method. The default implementation simply calls through to that method.

Unit callActivityOnStart(activity: Activity!)

Perform calling of an activity's Activity.onStart method. The default implementation simply calls through to that method.

Unit callActivityOnStop(activity: Activity!)

Perform calling of an activity's Activity.onStop method. The default implementation simply calls through to that method.

Unit callActivityOnUserLeaving(activity: Activity!)

Perform calling of an activity's Activity.onUserLeaveHint method. The default implementation simply calls through to that method.

Unit callApplicationOnCreate(app: Application!)

Perform calling of the application's Application.onCreate method. The default implementation simply calls through to that method.

Note: This method will be called immediately after onCreate(android.os.Bundle). Often instrumentation tests start their test thread in onCreate(); you need to be careful of races between these. (Well between it and everything else, but let's start here.)

Boolean checkMonitorHit(monitor: Instrumentation.ActivityMonitor!, minHits: Int)

Test whether an existing ActivityMonitor has been hit. If the monitor has been hit at least minHits times, then it will be removed from the activity monitor list and true returned. Otherwise it is left as-is and false is returned.

Unit endPerformanceSnapshot()

Unit finish(resultCode: Int, results: Bundle!)

Terminate instrumentation of the application. This will cause the application process to exit, removing this instrumentation from the next time the application is started. If multiple processes are currently running for this instrumentation, all of those processes will be killed.

Bundle! getAllocCounts()

Returns a bundle with the current results from the allocation counting.

Bundle! getBinderCounts()

Returns a bundle with the counts for various binder counts for this process. Currently the only two that are reported are the number of send and the number of received transactions.

ComponentName! getComponentName()

Returns complete component name of this instrumentation.

Context! getContext()

Return the Context of this instrumentation's package. Note that this is often different than the Context of the application being instrumentated, since the instrumentation code often lives is a different package than that of the application it is running against. See getTargetContext to retrieve a Context for the target application.

String! getProcessName()

Return the name of the process this instrumentation is running in. Note this should only be used for testing and debugging. If you are thinking about using this to, for example, conditionalize what is initialized in an Application class, it is strongly recommended to instead use lazy initialization (such as a getter for the state that only creates it when requested). This can greatly reduce the work your process does when created for secondary things, such as to receive a broadcast.

Context! getTargetContext()

Return a Context for the target application being instrumented. Note that this is often different than the Context of the instrumentation code, since the instrumentation code often lives is a different package than that of the application it is running against. See getContext to retrieve a Context for the instrumentation code.

UiAutomation! getUiAutomation()

Gets the UiAutomation instance with no flags set.

Note: The APIs exposed via the returned UiAutomation work across application boundaries while the APIs exposed by the instrumentation do not. For example, Instrumentation.sendPointerSync(MotionEvent) will not allow you to inject the event in an app different from the instrumentation target, while UiAutomation.injectInputEvent(android.view.InputEvent, boolean) will work regardless of the current application.

A typical test case should be using either the UiAutomation or Instrumentation APIs. Using both APIs at the same time is not a mistake by itself but a client has to be aware of the APIs limitations.

Equivalent to getUiAutomation(0). If a UiAutomation exists with different flags, the flags on that instance will be changed, and then it will be returned.

Compatibility mode: This method is infallible for apps targeted for Build.VERSION_CODES.R and earlier versions; for apps targeted for later versions, it will return null if UiAutomation fails to connect. The caller can check the return value and retry on error.

UiAutomation! getUiAutomation(flags: Int)

Gets the UiAutomation instance with flags set.

Note: The APIs exposed via the returned UiAutomation work across application boundaries while the APIs exposed by the instrumentation do not. For example, Instrumentation.sendPointerSync(MotionEvent) will not allow you to inject the event in an app different from the instrumentation target, while UiAutomation.injectInputEvent(android.view.InputEvent, boolean) will work regardless of the current application.

A typical test case should be using either the UiAutomation or Instrumentation APIs. Using both APIs at the same time is not a mistake by itself but a client has to be aware of the APIs limitations.

If a UiAutomation exists with different flags, the flags on that instance will be changed, and then it will be returned.

Compatibility mode: This method is infallible for apps targeted for Build.VERSION_CODES.R and earlier versions; for apps targeted for later versions, it will return null if UiAutomation fails to connect. The caller can check the return value and retry on error.

Boolean invokeContextMenuAction(targetActivity: Activity!, id: Int, flag: Int)

Show the context menu for the currently focused view and executes a particular context menu item.

Boolean invokeMenuActionSync(targetActivity: Activity!, id: Int, flag: Int)

Execute a particular menu item.

Boolean isProfiling()

Check whether this instrumentation was started with profiling enabled.

Activity! newActivity(clazz: Class<*>!, context: Context!, token: IBinder!, application: Application!, intent: Intent!, info: ActivityInfo!, title: CharSequence!, parent: Activity!, id: String!, lastNonConfigurationInstance: Any!)

Perform instantiation of an Activity object. This method is intended for use with unit tests, such as android.test.ActivityUnitTestCase. The activity will be useable locally but will be missing some of the linkages necessary for use within the system.

Activity! newActivity(cl: ClassLoader!, className: String!, intent: Intent!)

Perform instantiation of the process's Activity object. The default implementation provides the normal system behavior.

Application! newApplication(cl: ClassLoader!, className: String!, context: Context!)

Perform instantiation of the process's Application object. The default implementation provides the normal system behavior.

Application! newApplication(clazz: Class<*>!, context: Context!)

Perform instantiation of the process's Application object. The default implementation provides the normal system behavior.

Unit onDestroy()

Called when the instrumented application is stopping, after all of the normal application cleanup has occurred.

Boolean onException(obj: Any!, e: Throwable!)

This is called whenever the system captures an unhandled exception that was thrown by the application. The default implementation simply returns false, allowing normal system handling of the exception to take place.

Unit removeMonitor(monitor: Instrumentation.ActivityMonitor!)

Remove an ActivityMonitor that was previously added with #addMonitor.

Unit resetInTouchMode()

Resets the touch mode to the device default.

Unit runOnMainSync(runner: Runnable!)

Execute a call on the application's main thread, blocking until it is complete. Useful for doing things that are not thread-safe, such as looking at or modifying the view hierarchy.

Unit sendCharacterSync(keyCode: Int)

Sends up and down key events with the given key code to the currently focused window, and waits for it to be processed.

Equivalent to sendKeyDownUpSync(int).

Unit sendKeyDownUpSync(keyCode: Int)

Sends up and down key events with the given key code to the currently focused window, and waits for it to be processed.

Unit sendKeySync(event: KeyEvent!)

Sends a key event to the currently focused window, and waits for it to be processed.

This method blocks until the recipient has finished handling the event. Note that the recipient may not have completely finished reacting from the event when this method returns. For example, it may still be in the process of updating its display or UI contents upon reacting to the injected event.

Unit sendPointerSync(event: MotionEvent!)

Dispatches a pointer event into a window owned by the instrumented application, and waits for it to be processed.

If the motion event being injected is targeted at a window that is not owned by the instrumented application, the input injection will fail. See getUiAutomation() for injecting events into all windows.

This method blocks until the recipient has finished handling the event. Note that the recipient may not have completely finished reacting from the event when this method returns. For example, it may still be in the process of updating its display or UI contents upon reacting to the injected event.

Unit sendStatus(resultCode: Int, results: Bundle!)

Provide a status report about the application.

Unit sendStringSync(text: String!)

Sends the key events that result in the given text being typed into the currently focused window, and waits for it to be processed.

Unit sendTrackballEventSync(event: MotionEvent!)

Dispatches a trackball event into the currently focused window, and waits for it to be processed.

This method blocks until the recipient has finished handling the event. Note that the recipient may not have completely finished reacting from the event when this method returns. For example, it may still be in the process of updating its display or UI contents upon reacting to the injected event.

Unit setAutomaticPerformanceSnapshots()

Unit setInTouchMode(inTouch: Boolean)

Force the global system in or out of touch mode. This can be used if your instrumentation relies on the UI being in one more or the other when it starts.

Note: Starting from Android Build.VERSION_CODES.TIRAMISU, this method will only take effect if the instrumentation was sourced from a process with MODIFY_TOUCH_MODE_STATE internal permission granted (shell already have it).

Unit start()

Create and start a new thread in which to run instrumentation. This new thread will call to onStart where you can implement the instrumentation.

Activity! startActivitySync(intent: Intent!)

Start a new activity and wait for it to begin running before returning. In addition to being synchronous, this method as some semantic differences from the standard android.content.Context#startActivity call: the activity component is resolved before talking with the activity manager (its class name is specified in the Intent that this method ultimately starts), and it does not allow you to start activities that run in a different process. In addition, if the given Intent resolves to multiple activities, instead of displaying a dialog for the user to select an activity, an exception will be thrown.

The function returns as soon as the activity goes idle following the call to its android.app.Activity#onCreate. Generally this means it has gone through the full initialization including Activity.onResume and drawn and displayed its initial window.

Activity startActivitySync(intent: Intent, options: Bundle?)

Start a new activity and wait for it to begin running before returning. In addition to being synchronous, this method as some semantic differences from the standard android.content.Context#startActivity call: the activity component is resolved before talking with the activity manager (its class name is specified in the Intent that this method ultimately starts), and it does not allow you to start activities that run in a different process. In addition, if the given Intent resolves to multiple activities, instead of displaying a dialog for the user to select an activity, an exception will be thrown.

The function returns as soon as the activity goes idle following the call to its android.app.Activity#onCreate. Generally this means it has gone through the full initialization including Activity.onResume and drawn and displayed its initial window.

Unit startAllocCounting()

Unit startPerformanceSnapshot()

Unit startProfiling()

This method will start profiling if isProfiling() returns true. You should only call this method if you set the handleProfiling attribute in the manifest file for this Instrumentation to true.

Unit stopAllocCounting()

Unit stopProfiling()

Stops profiling if isProfiling() returns true.

Unit waitForIdle(recipient: Runnable!)

Schedule a callback for when the application's main thread goes idle (has no more events to process).

Unit waitForIdleSync()

Synchronously wait for the application to be idle. Can not be called from the main application thread -- use start to execute instrumentation in its own thread.

Activity! waitForMonitor(monitor: Instrumentation.ActivityMonitor!)

Wait for an existing ActivityMonitor to be hit. Once the monitor has been hit, it is removed from the activity monitor list and the first created Activity object that matched it is returned.

Activity! waitForMonitorWithTimeout(monitor: Instrumentation.ActivityMonitor!, timeOut: Long)

Wait for an existing ActivityMonitor to be hit till the timeout expires. Once the monitor has been hit, it is removed from the activity monitor list and the first created Activity object that matched it is returned. If the timeout expires, a null object is returned.

Constants

REPORT_KEY_NAME_CLASS

Added in API level 1
static val REPORT_KEY_NAME_CLASS: String

Deprecated: Deprecated in Java.

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the name of the current test class. This is sent with any status message describing a specific test being started or completed.

Value: "class"

REPORT_KEY_NAME_TEST

Added in API level 1
static val REPORT_KEY_NAME_TEST: String

Deprecated: Deprecated in Java.

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the name of the current test. This is sent with any status message describing a specific test being started or completed.

Value: "test"

REPORT_KEY_NUM_CURRENT

Added in API level 1
static val REPORT_KEY_NUM_CURRENT: String

Deprecated: Deprecated in Java.

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the sequence number of the current test. This is sent with any status message describing a specific test being started or completed.

Value: "current"

REPORT_KEY_NUM_TOTAL

Added in API level 1
static val REPORT_KEY_NUM_TOTAL: String

Deprecated: Deprecated in Java.

If included in the status or final bundle sent to an IInstrumentationWatcher, this key identifies the total number of tests that are being run. This is sent with all status messages.

Value: "numtests"

REPORT_KEY_STACK

Added in API level 1
static val REPORT_KEY_STACK: String

Deprecated: Deprecated in Java.

If included in the status bundle sent to an IInstrumentationWatcher, this key identifies a stack trace describing an error or failure. This is sent with any status message describing a specific test being completed.

Value: "stack"

REPORT_VALUE_ID

Added in API level 1
static val REPORT_VALUE_ID: String

Deprecated: Deprecated in Java.

This value, if stored with key android.app.Instrumentation#REPORT_KEY_IDENTIFIER, identifies InstrumentationTestRunner as the source of the report. This is sent with all status messages.

Value: "InstrumentationTestRunner"

REPORT_VALUE_RESULT_ERROR

Added in API level 1
static val REPORT_VALUE_RESULT_ERROR: Int

Deprecated: Deprecated in Java.

The test completed with an error.

Value: -1

REPORT_VALUE_RESULT_FAILURE

Added in API level 1
static val REPORT_VALUE_RESULT_FAILURE: Int

Deprecated: Deprecated in Java.

The test completed with a failure.

Value: -2

REPORT_VALUE_RESULT_OK

Added in API level 1
static val REPORT_VALUE_RESULT_OK: Int

Deprecated: Deprecated in Java.

The test completed successfully.

Value: 0

REPORT_VALUE_RESULT_START

Added in API level 1
static val REPORT_VALUE_RESULT_START: Int

Deprecated: Deprecated in Java.

The test is starting.

Value: 1

Public constructors

InstrumentationTestRunner

InstrumentationTestRunner()

Public methods

getAllTests

Added in API level 1
open fun getAllTests(): TestSuite!

Deprecated: Deprecated in Java.

Override this to define all of the tests to run in your package.

getArguments

Added in API level 18
Deprecated in API level 24
open fun getArguments(): Bundle!

Deprecated: Deprecated in Java.

Get the arguments passed to this instrumentation.

Return
Bundle! the Bundle object

getLoader

Added in API level 1
open fun getLoader(): ClassLoader!

Deprecated: Deprecated in Java.

Override this to provide access to the class loader of your package.

getTestSuite

Added in API level 1
open fun getTestSuite(): TestSuite!

Deprecated: Deprecated in Java.

onCreate

Added in API level 1
open fun onCreate(arguments: Bundle!): Unit

Deprecated: Deprecated in Java.

Parameters
arguments Bundle!: Any additional arguments that were supplied when the instrumentation was started.

onStart

Added in API level 1
open fun onStart(): Unit

Deprecated: Deprecated in Java.

Protected methods

addTestListener

Added in API level 1
protected open fun addTestListener(listener: TestListener!): Unit

Deprecated: Deprecated in Java.

Add a TestListener

getAndroidTestRunner

Added in API level 1
protected open fun getAndroidTestRunner(): AndroidTestRunner!

Deprecated: Deprecated in Java.