Posted By: Igor Escodro
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.
android { | |
testOptions { | |
devices { | |
pixel2api29(com.android.build.api.dsl.ManagedVirtualDevice) { | |
device = "Pixel 2" | |
apiLevel = 29 | |
systemImageSource = "google" | |
abi = "x86" | |
} | |
} | |
} | |
} |
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.
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:
testOptions { | |
devices { | |
pixel2api29 (com.android.build.api.dsl.ManagedVirtualDevice) { ... } | |
nexus9api30 (com.android.build.api.dsl.ManagedVirtualDevice) { ... } | |
deviceGroups { | |
phoneAndTablet { | |
targetDevices.addAll(devices.pixel2api29, devices.nexus9api30) | |
} | |
} | |
} | |
} |
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
Job Offers
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. ?
igorescodro/virtual-device-test
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. ❤️
Thanks to Bruno Kenji Tiba.