public class AndroidJUnitRunner
extends MonitoringInstrumentation


一个 Instrumentation运行针对Android包(应用程序)JUnit3和JUnit4测试。

Based on and replacement for InstrumentationTestRunner. Supports a superset of InstrumentationTestRunner features, while maintaining command/output format compatibility with that class.

Typical Usage

Write JUnit3 style TestCases and/or JUnit4 style Tests that perform tests against the classes in your package. Make use of the InstrumentationRegistry if needed.

In an appropriate AndroidManifest.xml, define an instrumentation with android:name set to AndroidJUnitRunner and the appropriate android:targetPackage set.

Execution options:

Running all tests: adb shell am instrument -w

Running all tests in a class: adb shell am instrument -w -e class

Running a single test: adb shell am instrument -w -e class

Running all tests in multiple classes: adb shell am instrument -w -e class,

Running all tests except those in a particular class: adb shell am instrument -w -e notClass

Running all but a single test: adb shell am instrument -w -e notClass

Running all tests listed in a file: adb shell am instrument -w -e testFile /sdcard/tmp/testFile.txt The file should contain a list of line separated test classes and optionally methods (expected format:

Running all tests in a java package: adb shell am instrument -w -e package

Running all tests except a particular package: adb shell am instrument -w -e notPackage

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

Running a specific test size i.e. annotated with SmallTest or MediumTest or LargeTest: adb shell am instrument -w -e size [small|medium|large]

Filter test run to tests with given annotation: adb shell am instrument -w -e annotation

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

Filter test run to tests without given annotation: adb shell am instrument -w -e notAnnotation

As above, if used with other options, the resulting test run will contain the intersection of the two options. e.g. "-e size large -e notAnnotation" will run tests with the LargeTest annotation that do NOT have the "" annotations.

Filter test run to tests without any of a list of annotations: adb shell am instrument -w -e notAnnotation,

Filter test run to a shard of all tests, where numShards is an integer greater than 0 and shardIndex is an integer between 0 (inclusive) and numShards (exclusive): adb shell am instrument -w -e numShards 4 -e shardIndex 1

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 code coverage files (*.ec) that can be used by EMMA or JaCoCo: -e coverage true Note: For this to work, your classes have to be instrumented offline (i.e. at build time) by EMMA/JaCoCo. By default, the code coverage results file will be saved in a /data/data/ /files/ file, unless overridden by coverageFile flag (see below)

To specify EMMA code coverage results file path: -e coverageFile /sdcard/

To specify one or more RunListeners to observe the test run: -e listener,

Set timeout (in milliseconds) that will be applied to each test: -e timeout_msec 5000

Supported for both JUnit3 and JUnit4 style tests. For JUnit3 tests, this flag is the only way to specify timeouts. For JUnit4 tests, this flag overrides timeouts specified via org.junit.rules.Timeout. Please note that in JUnit4 org.junit.Test#timeout() annotation take precedence over both, this flag and org.junit.Test#timeout() annotation.

To disable Google Analytics: -e disableAnalytics true

In order to make sure we are on the right track with each new release, the test runner collects analytics. More specifically, it uploads a hash of the package name of the application under test for each invocation. This allows us to measure both the count of unique packages using this library as well as the volume of usage.

All arguments can also be specified in the in the AndroidManifest via a meta-data tag: eg. using listeners: instrumentation android:name="" ... meta-data android:name="listener" android:value="," Arguments specified via shell will take override manifest specified arguments.


Inherited constants

From class

Public constructors


Public methods

void finish(int resultCode, Bundle results)


void onCreate(Bundle arguments)


boolean onException(Object obj, Throwable e)
void onStart()


Inherited methods

From class
From class
From class java.lang.Object

Public constructors


AndroidJUnitRunner ()

Public methods


void finish (int resultCode, 
                Bundle results)


覆盖此方法的子类应完成其处理,然后调用super.finish来调用此逻辑。 在退出前不等待所有活动完成()会导致设备宽泛不稳定。

resultCode int
results Bundle


void onCreate (Bundle arguments)


子类必须调用onCreate()。 这个onCreate方法不会调用start(),如果需要,它是子类的责任。

arguments Bundle


boolean onException (Object obj, 
                Throwable e)

obj Object
e Throwable


void onStart ()