page.title=Testing from the Command-Line @jd:body

In this document

  1. Running Tests
    1. Running unit tests with Gradle
    2. Running tests with adb
  2. Using the Instrument Command
    1. Instrument options
    2. Instrument examples

See Also

  1. Android Debug Bridge

This document describes how to create and run tests directly from the command line. This document assumes that you already know how to create a Android application in your programming environment.

Running Tests

You can run tests from the command-line, either with Gradle or with an Android Debug Bridge (adb) shell.

Running unit tests with Gradle

The Android Plugin for Gradle lets you run unit tests from your Gradle project via the command-line. For more information on how to build unit tests for your app, see Building Effective Unit Tests.

The table below summarizes how to run your unit tests with Gradle:

Unit Test Type Command To Run Test Result Location
Local unit test Call the {@code test} task:
./gradlew test
HTML test result files: {@code <path_to_your_project>/app/build/reports/tests/} directory.

XML test result files: {@code <path_to_your_project>/app/build/test-results/} directory.

Instrumented unit test Call the {@code connectedAndroidTest} (or {@code cAT}) task:
./gradlew cAT
HTML test result files: {@code <path_to_your_project>/app/build/outputs/reports/androidTests/connected/} directory.

XML test result files: {@code <path_to_your_project>/app/build/outputs/androidTest-results/connected/} directory.

Running tests with ADB

When you run tests from the command-line with Android Debug Bridge (adb), you get more options for choosing the tests to run than with any other method. You can select individual test methods, filter tests according to their annotation, or specify testing options. Since the test run is controlled entirely from a command-line, you can customize your testing with shell scripts in various ways.

To run a test from the command-line, you run adb shell to start a command-line shell on your device or emulator, and then in the shell run the am instrument command. You control am and your tests with command-line flags.

As a shortcut, you can start an adb shell, call am instrument, and specify command-line flags all on one input line. The shell opens on the device or emulator, runs your tests, produces output, and then returns to the command-line on your computer.

To run a test with am instrument:

  1. If necessary, rebuild your main application and test package.
  2. Install your test package and main application Android package files (.apk files) to your current Android device or emulator
  3. At the command-line, enter:
    $ adb shell am instrument -w <test_package_name>/<runner_class>
    

    where <test_package_name> is the Android package name of your test application, and <runner_class> is the name of the Android test runner class you are using. The Android package name is the value of the package attribute of the manifest element in the manifest file (AndroidManifest.xml) of your test package. The Android test runner class is usually {@code AndroidJUnitRunner}.

    Your test results appear in STDOUT.

This operation starts an adb shell, then runs am instrument with the specified parameters. This particular form of the command will run all of the tests in your test package. You can control this behavior with flags that you pass to am instrument. These flags are described in the next section.

Using the am instrument Command

The general syntax of the am instrument command is:

am instrument [flags] <test_package>/<runner_class>

The main input parameters to am instrument are described in the following table:

Parameter Value Description
<test_package> The Android package name of the test package. The value of the package attribute of the manifest element in the test package's manifest file.
<runner_class> The class name of the instrumented test runner you are using. This is usually {@code AndroidJUnitRunner}.

The flags for am instrument are described in the following table:

Flag Value Description
-w (none) Forces am instrument to wait until the instrumentation terminates before terminating itself. The net effect is to keep the shell open until the tests have finished. This flag is not required, but if you do not use it, you will not see the results of your tests.
-r (none) Outputs results in raw format. Use this flag when you want to collect performance measurements, so that they are not formatted as test results. This flag is designed for use with the flag -e perf true (documented in the section Instrument options).
-e <test_options> Provides testing options as key-value pairs. The am instrument tool passes these to the specified instrumentation class via its onCreate() method. You can specify multiple occurrences of -e <test_options>. The keys and values are described in the section am instrument options. You can only use these key-value pairs with {@code AndroidJUnitRunner} or with {@link android.test.InstrumentationTestRunner} and its subclasses. Using them with any other class has no effect.

am instrument options

The am instrument tool passes testing options to {@code AndroidJUnitRunner} or {@link android.test.InstrumentationTestRunner} in the form of key-value pairs, using the -e flag, with this syntax:

-e <key> <value>

Some keys accept multiple values. You specify multiple values in a comma-separated list. For example, this invocation of {@code AndroidJUnitRunner} provides multiple values for the package key:

$ adb shell am instrument -w -e package com.android.test.package1,com.android.test.package2 \
> com.android.test/android.support.test.runner.AndroidJUnitRunner

The following table lists the key-value pairs you can use with your test runner.

Key Value Description
package <Java_package_name> The fully-qualified Java package name for one of the packages in the test application. Any test case class that uses this package name is executed. Notice that this is not an Android package name; a test package has a single Android package name but may have several Java packages within it.
class <class_name> The fully-qualified Java class name for one of the test case classes. Only this test case class is executed.
<class_name>#method name A fully-qualified test case class name, and one of its methods. Only this method is executed. Note the hash mark (#) between the class name and the method name.
func true Runs all test classes that extend {@link android.test.InstrumentationTestCase}.
unit true Runs all test classes that do not extend either {@link android.test.InstrumentationTestCase} or {@link android.test.PerformanceTestCase}.
size [small | medium | large] Runs a test method annotated by size. The annotations are @SmallTest, @MediumTest, and @LargeTest.
perf true Runs all test classes that implement {@link android.test.PerformanceTestCase}. When you use this option, also specify the -r flag for am instrument, so that the output is kept in raw format and not re-formatted as test results.
debug true Runs tests in debug mode.
log true Loads and logs all specified tests, but does not run them. The test information appears in STDOUT. Use this to verify combinations of other filters and test specifications.
emma true Runs an EMMA code coverage analysis and writes the output to /data/<app_package>/coverage.ec on the device. To override the file location, use the coverageFile key that is described in the following entry.

Note: This option requires an EMMA-instrumented build of the test application, which you can generate with the coverage target.

coverageFile <filename> Overrides the default location of the EMMA coverage file on the device. Specify this value as a path and filename in UNIX format. The default filename is described in the entry for the emma key.
-e Flag Usage Notes

Usage examples

The following sections provide examples of using am instrument to run tests. They are based on the following structure:

Running the entire test package

To run all of the test classes in the test package, enter:

$ adb shell am instrument -w com.android.demo.app.tests/android.support.test.runner.AndroidJUnitRunner

Running all tests in a test case class

To run all of the tests in the class UnitTests, enter:

$ adb shell am instrument -w  \
> -e class com.android.demo.app.tests.Foo \
> com.android.demo.app.tests/android.support.test.runner.AndroidJUnitRunner

am instrument gets the value of the -e flag, detects the class keyword, and runs all the methods in the UnitTests class.

Selecting a subset of tests

To run all of the tests in Foo1, and the bar3 method in Foo2, enter:

$ adb shell am instrument -w \
> -e class com.android.demo.app.tests.Foo1,com.android.demo.app.tests.Foo2#bar3 \
> com.android.demo.app.tests/android.support.test.runner.AndroidJUnitRunner