Disabling animations when running Android instrumented tests

A reliable and extensible approach

Photo by Isaac Li Shung Tan on Unsplash

Overview

Extensibility note: you can add more delegates for other functionality like automatically unlocking your device before running a test.

Step 1: Custom Test Runner

testInstrumentationRunner androidx.test.runner.AndroidJUnitRunner
CustomAndroidTestRunner template

Note: If you are setting up a new project without any instrumented tests, check that you have espresso-core dependency in your build.gradle.

testInstrumentationRunner full.package.name.of.your.CustomTestRunner

Step 2: Test Runner Delegates

CustomAndroidTestRunner implementation

Note that there is no way to provide constructor arguments to the test runner in Gradle. So we have to provide a delegates list as a default constructor argument.

Step 3: AnimationsTestRunnerDelegate

InstrumentationRegistry.getInstrumentation().uiAutomation
.executeShellCommand("%some command%")

Note: this is an equivalent of invoking “adb shell %some command%” on your laptop’s CLI.

"settings get global animator_duration_scale""settings put global animator_duration_scale $value""settings put global transition_animation_scale $value""settings put global window_animation_scale $value"
AnimationsTestRunnerDelegate that enables/disables device animations

Note: I have intentionally left out exception handling for clarity of the example.

Conclusion

--

--

Self-Employed Senior Android Dev | https://romantikonov.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store