How I tested my entire Android app, in just One hour

Kiven Aa — Android App’s Landing Screen

I don’t know about you, but a mere thought of testing an app, gives me a headache. But, we’ve to do the dirty work, there’s no escaping that. Technically, there is, but do you really want to release an untested product? I hope not.

So, I was hard at work, for a last couple of day, working on a native Android client for Kiven Aa. If you head over to Google Play, today (dated: 29 July, 2017), you’ll find Kiven Aa on the store but that app uses a WebView, to load the website and that’s it. If you’re reading this in future, you might not get an opportunity to try the WebView based version.

There were many shortcomings of a WebView based app and Native is the answer to all of those. You should also read, why I ditched React Native for Native.

Hence, a native version was inevitable. Now, I finished the Android counterpart yesterday and I want to release the app, ASAP

But wait! Did you test your app?

Umm, no! Of course, I conducted many manual tests while coding up the entire app but humans are not very good at remembering all corner cases which an app can encounter so performing all checks manually, after every little change I make to the app, won’t scale. I needed a test suite to take care of such things.

I have had some experience with JUnit but I had no idea how would that help in testing Android apps, because I’ve never written test suites (until today!) for my Android apps. Future clients take note. But this time, I really wanted to test everything up, before releasing the app, out there in the wild.

Navigating to Android Developer Documentation was an obvious move, so I did that. There I found this new thing Espresso. I swear this thing was not around, when I was writing Pollen Chat, which is a hyper-local messaging app, in-case you don’t know.

For the uninitiated, Espresso is the UI test recorder which is now included in Android Studio, since version 2.3.

This is how it works

  1. You start recording the test.
  2. Espresso boots up the app, on your device.
  3. You carry out the operation, whatever they be, on your app.
  4. You tell Espresso what UI element it should look at, at the end of your test.
  5. Espresso parses your entire operation sequence like button presses etc. and generates a test file, which contains normal Java statements, which you can edit later on, to fine tune your test.
  6. Espresso saves the test to your test suite, for later consumption.

It was like a whole new way of testing, for me at-least, as I ain’t no tester, so my knowledge of testing tools is rather limited. I know that tools like Selenium do exist, but I don’t have much experience with such tools (Psst! I wrote a selenium based chat bot, a few days back.)

Once I got the hang of things, which literally took me about 10 seconds, after watching this video, I was on my way to test my app, in its entirety.

I started recording tests for every possible scenario, from the end user perspective and I was done in around 50 minutes. Well done! Espresso.

Having said that, Espresso is not the panacea, as of now, as it is, kind of, limited in its current form. You cannot test everything and sometimes you’ve to manually edit the generated test, to fit your needs. In my case, as the app interacts with an API, I needed to put a bunch of SystemClock.sleep() calls in my tests so as to let the app wait for the API, to return the results of API calls.

All in all, it was a great experience and I will dive deep into Espresso. I will come back with a detailed post about how to write Espresso tests for your Android app and probably make a video as well. So, please follow me if you would like to get notified when that post/video comes out.

If you liked this post, kindly recommend it to your network and follow me for more posts like this. You should also subscribe to my YouTube channel, if you like developing digital things.

Facebook|Twitter|YouTube

Till next time…