Running Instrumented Tests in a Gradle task

Igor Escodro
ProAndroidDev
Published in
3 min readJun 23, 2021

--

During the latest Google I/O, a lot of great new technologies were shown. The most exciting feature in my opinion was the Unified Test Platform (formerly known as Nitrogen), which first public steps were presented in the event. Starting from Android Studio Bumblebee, we can try experimental features for making our tests more reliable and scalable, specially for automated instrumented tests.

We were introduced to Gradle Managed Virtual Devices, a tool that allows running instrumented tests with the Android Gradle Plugin. This new feature is responsible for downloading, creating, deploying and shutting down a virtual device, executing tests with more performance and consistency.

Creating our first Virtual Device

In order to get started, all we have to do is specify the virtual device configurations in our build.gradle file.

Build Gradle configuring the Gradle Virtual Device

In the code above, we are creating a Pixel 2 device, running API level 29 with Google Play Services in a x86 ABI. We can mix and match devices like Phones, Tablets, TVs, Wear OS, Generic Phones with any version of Android.

For more information on which devices can be created, we can navigate to Tools > AVD Manager > Create Virtual Device… and check all the supported hardware options.

Android Virtual Device window
Android Virtual Device Manager

After the Virtual Device is set, we can run the command to make the automated tests run. First of all we enable the experimental Unified Test Platform feature and refer our device-name +BuildVariant + AndroidTest task. The generic command is:

./gradlew
-Pandroid.experimental.androidTest.useUnifiedTestPlatform=true
device-nameBuildVariantAndroidTest

Updating the command for our build.gradle created earlier to run the test in Debug, will look like:

./gradlew
-Pandroid.experimental.androidTest.useUnifiedTestPlatform=true
pixel2api29DebugAndroidTest

Creating a group of devices

We can also create a group of devices with different setups in order to validate our tests in multiple scenarios. All we have to do is add a new deviceGroups parameter, name it and link the created devices on it. Let’s take a look in the example below:

Build Gradle configuring the Devices Groups

Now, in our example we have a new tablet device (Nexus 9) and a new device group named phoneAndTablet was created . Inside this new group, we link our two created devices to run together. To run our tests in the device group, we update the previous run command with the device-group + Group instead.

./gradlew
-Pandroid.experimental.androidTest.useUnifiedTestPlatform=true
phoneAndTabletGroupDebugAndroidTest

What’s next?

I created a repository to make it easier to test this new amazing feature. All you have to do is clone, compile using Android Studio Bumblebee and run any of the commands in this article to test it. 🎉

Conclusion

I’ve worked in several projects where we created instrumented tests, but they were not executed in a pipeline due to some technical restrictions and the difficult to integrate the Android Emulator with some Continuous Integration services. Now, using the Gradle Managed Virtual Devices, the execution is as simple as adding a new task to run on your build.

I’m very excited about how the Unified Test Platform will help us create better tests and environments to validate our application in different form factors and APIs. All of this, with simple parameters on our Gradle file.

If you face any issue using the new experimental tools, feel free to open an issue on Issue Tracker. It will help Google track and fix the bugs and help the community creating a better solution. 😊

External resources

For more information regarding the Android Studio Bumblebee features and how Unified Test Platform can help you achieve more reliability and scalability, I recommend the following links:

Thanks a lot for reading my article. ❤️

--

--

Passionate Android developer | Google Developer Expert for Android