Power of Android Test Orchestrator for OOM (Out Of Memory) error while running espresso test cases

Pooja Londhe
Globant
Published in
2 min readJun 7, 2020
Gif from: https://www.journaldev.com/23067/android-espresso

When we have more than 150–200 automation test cases written for our application which takes around 1 to 1.5 hours to run completely. There we can face an issue of OOM (Out Of Memory) error while running test cases.

Basically, when running multiple test cases one after another, the heap size of the device continuously increases, and after some point, it may cause an OOM error.

You might try multiple solutions like :

  1. Increasing memory of RAM
  2. Dividing the test cases by module

Android Test Orchestrator can help us with that

Applying Android Test Orchestrator from scratch in our projects increased the reliability of our automated test suites.

Android Test orchestrator provides a flag to clear all the package data after the completion of every test case.

testInstrumentationRunnerArguments clearPackageData: 'true'

When you add test orchestrator with the above flag, it removes all the shared state from your device’s CPU and memory after each test.

If there is an app crash in the application all the test suit gets stop without any test assertion failure report. This crash sometimes is a headache as we are running multiple test cases altogether.

As Android Test Orchestrator creates a separate instance of the instrumentation. So, if there is an app crash then this would crash only for current instrumentation and other tests would not be affected.

Ready to implement Android Test Orchestrator?

We are going to enable the android test orchestrator from the Gradle.

Add the following lines to your Gradle file

android {  
defaultConfig {

...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {

execution 'ANDROID_TEST_ORCHESTRATOR'
}
}
dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
}

Note: Check and use the latest versions for the above dependencies. If you referred to android official documentation, you will notice there are android dependencies. If you are adding androidx dependencies please make sure that all application dependencies should be of androidx type.

Run Android Test Orchestrator by executing the following command from the command line:

./gradlew connectedCheck

Note: If you are using latest Gradle version then please update your project level Gradle like:

dependencies {    
classpath 'com.android.tools.build:gradle:4.0.0'
}

and run the above command. If you use Gradle build tools in any version below 3.0 you also have to update the dependency setup for your project. If your Gradle version is old you may found the error ‘No Test Found’. Please update your Gradle.

--

--