October 17, 2017   |   5min read

Keep Espresso Tests Stable with Android Test Orchestrator


If you have ever executed Espresso tests in your Android Project, I bet you had a lot flakiness and shared states in separated tests. In spite of using the tools that make tests more stable (read more about it in one of our previous posts by Tomasz), you can still experience some kind of instability.

Fortunately, Google has responded to the problem. Since the Android Testing Support Library 1.0 is here, we’ve got a perfect solutions to all those problems—Android Test Orchestrator.

Android Test Orchestrator is a gradle test option that helps in testing thanks to the supporting packages available on the maven repository. Applying Android Test Orchestrator from scratch in our projects increased the reliability of our automated test suites.

In this article, I would like to show you an example of how this new Android tool can be used for Gradle builds.

Android Studio and Gradle dependencies

The support for Android Test Orchestrator arrived with Android Studio 3.0 beta build, so for now, it is needed to use IDE from that release channel.

That’s not all. If you use Gradle build tools in any version below 3.0 you also have to update dependency setup for your project:

classpath ''

In order to configure latest Espresso (3.0.2) with Android Test Orchestrator put these dependencies in your app/build.gradle file:

testImplementation 'junit:junit:4.13'
androidTestImplementation ''
androidTestImplementation ''
androidTestUtil '

With Android Test Support Library 1.0 comes the androidTestImplementation keyword which resolve dependency apk file and install via adb.

Solution #1 Running test with Android Test Orchestrator from command line

Based on Google’s documentation, enabling Android Test Orchestrator for older gradle build tools versions (not supporting androidTestUtil) involves a different approach. In order to do it, one needs to call install steps for Orchestrator and Test-services apk files and execute shell commands with specified test runner. You can put those files in the directory inside project repository (it is about 1 MB) and trigger each test execution with the set of commands:

adb install ./apk_directory/orchestrator-1.0.2.apk
adb install ./apk_directory/test-services-1.0.2.apk
adb shell 'CLASSPATH=$(pm path app_process / \ am instrument -w -e -v \

The thing that can be regarded as inconvenient in this scenario is that you can’t use Fastlane for tests execution, therefore you still face some problems with sharing the results in a smart way. In order to get some statistics from the execution, you will need to flush the output of the third command and get the results in a programmatic manner.

Solution #2 Based on Build.gradle

The recommended setup claims to add a single line to the testOptions section inside the build.gradle module related file:

 testOptions {

That is really simple. If you don’t want to let Android Test Orchestrator to be on in each test run you can overcome this by passing the optional argument for gradle task e.g.:

./gradlew connectedAndroidTest -Porchestrator=true

You can specify the project variables inside the build.gradle project related file:

project.ext {
    orchestrator = project.hasProperty('orchestrator') ?'orchestrator') : false

And then determine testOptions on orchestrator variable value:

 testOptions {
        if (orchestrator == true) {
            println("With android test orchestrator")
            execution "ANDROID_TEST_ORCHESTRATOR"

Thanks to this setup instruction you will get Android Test Orchestrator disabled. Also, you will be able to run configurations without orchestrator=true argument passed for Android Studio. Android Test Orchestrator will be enabled for all executions with that argument passed — using command line or just “Run” button in Android Studio. After this you will get an output from all required steps attached to the mechanism usage:

Testing started at 14:00 ...

09/19 14:00:02: Launching MainActivityTest
$ adb push .../app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/YOUR_PACKAGE_NAME
$ adb shell pm install -t -r "/data/local/tmp/YOUR_PACKAGE_NAME"

$ adb push .../app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk /data/local/tmp/YOUR_PACKAGE_NAME.test
$ adb shell pm install -t -r "/data/local/tmp/YOUR_PACKAGE_NAME.test"

$ adb push .../orchestrator-1.0.2.apk /data/local/tmp/
$ adb shell pm install -t -r "/data/local/tmp/"

$ adb push .../test-services-1.0.1.apk /data/local/tmp/
$ adb shell pm install -t -r "/data/local/tmp/"

Running tests

$ adb shell CLASSPATH=$(pm path app_process / am instrument -r -w -e targetInstrumentation YOUR_PACKAGE_NAME.test/YOUR_PACKAGE_NAME.BaseAndroidJUnitRunner   -e debug false -e class YOUR_PACKAGE_NAME.MainActivityTest

Client not ready yet..
Started running tests
Tests ran to completion.

Fastlane integration for running suites with Android Test Orchestrator

Thanks to Gradle plugin for Fastlane we can enjoy the simplicity of lanes also as far as Android builds are concerned. We can use project variable defined in the previous scope and execute Gradle task with the following argument passed:

desc "Runs orchestrator tests"
   lane :orchestrator do
       gradle(task: "connectedAndroidTest", properties: {
         "orchestrator" => true


The malformed test sessions had been pain in the neck to everyone who wrote them until now. With a great tool from Google we can forget about all of the shared states problems forever. I hope you’ve enjoyed my tutorial and won’t have any issues with Android Test Orchestrator in your project!

Adam Stasiak

Senior Test Engineer

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!