I am blogging this just to make my notes on Android Testing. Nearly all the things here have been mentioned by other developers and google guys.

Before android unit test support been added to gradle and Android Studio, my android tests were separated into two tiers

Simple unit tests, which don’t rely on Android classes. Integration, UI and unit-tests which use android classes.

I made separate modules for the housing the classes and packages which just depended on plain old java. For example for fetching data from server I have seperate java module (not android module), normally named ‘api’. As it is a java module one can do the simple java junit tests on it.

I used to use robolectric but ceased to use robolectric for android testing after gradle and Android Studio came out (I am reconsidering using Robolectric after its version 3.0 came out). Robolectric version 2.4 supported till android api-18. To run the test with gradle required a now depricated gradle plugin robolectric-gradle-plugin and https://github.com/JCAndKSolutions/android-unit-test and https://github.com/evant/android-studio-unit-test-plugin and required many more configurations to be done. One couldn’t even run the tests from old Android Studio (v 1.0). But doing testing on android was still pain in the ass.

Damn Slow Emulator

Running the tests on emulator had more problems. The non hardware accelerated emulator is so slow to start that often tests timed out on our CI server (We have jenkins installed on xCloud) or travis-ci or circle-ci. And whenever they ran they took a long time.

Voila — Hardware Accelerated Emulators

The Intel Hardware Accelerated Execution Manager (Intel® HAXM) is a hardware-assisted virtualization engine (hypervisor) that uses Intel Virtualization Technology (Intel® VT) to speed up Android app emulation on a host machine. In combination with Android x86 emulator images provided by Intel and the officialAndroid SDK Manager, HAXM allows for faster Android emulation on Intel VT enabled systems.

From — https://software.intel.com/en-us/android/articles/intel-hardware-accelerated-execution-manager

But VT is not enabled in virtual environment

We could not even use hardware accelerated emulators by using HAXM/KVM as it require real machines and not the virtual machines provided by xCloud or docker instances from travis-ci or circle-ci. Though, travis-ci are working on providing some solution for this — https://github.com/travis-ci/travis-ci/issues/1419.

Enters Unit Testing Support

With gradle and Android Studio 1.1 onwards we were given unit testing support. Details for it could be fetched here and here.

If one has some dependencies on android, One can mock android classes in these tests using Mockito.

Robolectric 3.0

Robolectric 3.0 gives out of box integration with Android studio and gradle and supports till api-21. So, using it for unit tests now is hassle free. So, I will be using it in my next project. What a relief.

So some links and resources for testing so that I don’t forget them

Samples by google

https://github.com/googlesamples/android-testing/

These samples are using — jUnit4, espresso, hamcrest.

Android Testing Blueprint https://plus.google.com/+AndroidDevelopers/posts/NPagF6bpHsv

Espresso —

https://code.google.com/p/android-test-kit/ —

Some more examples of espresso —

https://developer.android.com/training/testing/ui-testing/espresso-testing.html

Robolectric — Robolectric does the tests on JVM -

https://github.com/robolectric/robolectric —

Samples in robolectric’s repository

https://github.com/robolectric/robolectric-samples

Vogella

http://www.vogella.com/tutorials/Robolectric/article.html

Fragmented podcast on Android Testing — Thanks Kaushik Gopal

http://fragmentedpodcast.com/episodes/13/

Some blogs on Android

http://jakewharton.com/android-needs-a-simulator/

http://pivotallabs.com/simple-bdd-android-testing-with-robolectric/