Instrumentation 101 – Must have tips for Android developers

By December 18, 2012Events, Hacking

Thanks to everyone who came out to our class! It was great meeting and chatting with all of you Android developers. If you missed your chance to check out our 30 minute primer at AnDevCon IV, you’ll find the details of our Android Instrumentation 101 talk in this post along with download links for the demos and scripts used in the presentation. You can download the presentation here.

Testing can be boring and tedious, but its role is very important. In the long run you’ll save time fixing bugs in your application. Fortunately, Android supports many tools for testing. The ones we’ll cover include Monkey, MonkeyRunner, Robotium, and UIAutomator.


Monkey is a simple and easy to use command line tool that is part of the Android SDK.

Monkey’s only purpose is stress testing your application. This means testing the things you would normally not test or think to test. Monkey provides a stream of random input events while your application is running with the primary goal of determining how your app preforms under such a load. This will test all kinds of strange input events and I assure you, your app will crash at some point under a corner case that you never imagined.

Monkey is not for testing specific app features and it is not smart (smart being, scriptable – you don’t tell Monkey what to do). Finally, Monkey is not a “cure-all.” It won’t find the bugs that other types of testing will find.

Monkey is simple, easy, and while not terribly exciting, it’s most definitely useful.

Monkey in action.


In addition to stress testing, you may want to test a specific sequence of inputs. MonkeyRunner allows you to easily script your tests.

MonkeyRunner is a smarter Monkey in that it allows for better structured tests. You can do functional and regression testing using MonkeyRunner.
Some of its features include support for scripts written in Python, ability to take screenshots of the device, and the MonkeyRunner API is extensible. On the downside, it isn’t fast.

Things you’ll need to do:
• Learn Python
• Be aware that MonkeyRunner uses Jython. This means that your Python scripts can interface with java classes.

Understand how to use the three main modules of MonkeyRunner:
• MonkeyRunner – Connect to device
• MonkeyDevice – Call functions on the device (examples below)
• MonkeyImage – Take screenshots

The MonkeyRunner module allows your to connect to your device through a device object (MonkeyDevice). The device object allows you to call various methods, like:
• installPackage
• touch (pass in touch type)
• takeSnapshot
• startActivity

MonkeyRunner in action.


Robotium is a powerful tool and API for testing your Android applications.

Robotium is more in depth and more robust than the tools mentioned above. The basic idea of Robotium is to create a testing APK that will exercise your target app through test cases that you write. In that way, it is very much like any standard testing framework. Robotium’s tag line is, “Like Selenium for Android,” so if you come from a web development background Robotium should be familiar ground for you. One more thing to note, Robotium is very fast which is a great boon to the testing experience.

On the downside, Robotuim requires some boilerplate. You’re going to have to create an APK, build with the Robotium JAR, and in general, be aware of the internals of the app you wish to test. That being said, it is quite powerful and, some might say, the de-facto testing tool for Android apps.

Robotium requires the Robotium JAR which can easily be downloaded from the Robotium website.

You’re going to need to know your APK’s package name and signature, though, you should have both if you are developing the app you wish to test.

Tests are set up by extending the main test class and writing methods for each test case. The Robotium Solo class provides the interface for testing on the device. That is, all of your testing commands will be issued from a solo object. As is the norm with testing, you’ll want to use assert to ensure test results.

If you take a look at the API later you’ll see that Robotium provides a bunch of methods, allowing you to do a lot with the app (switching activities being quite useful).

Finally, Robotium can be run from Eclipse or the command line (your preference) and provides output regarding test case success, failure, and error.

Robotium in action. 


UIAutomator is a testing framework that has been added fairly recently.

It is part of the Android SDK revision 21 and up and requires your test device to have Android API version 16 or higher.

UIAutomator consists of:
• A command-line tool found on the device
• A Java library (uiautomator.jar found in the sdk/tools/) which you use in your tests
• The Android SDK tool UIAutomatorViewer which comes in handy for writing your tests
• The ability to reference UI Objects by name

UIAutomator comes with the UIAutomatorViewer tool that captures screenshot of the device and lists all the UI objects (great for identifying which components you want to test).

UIAutomater in action.

To see examples on using each tool, here are some of our presentation files:

So far the tools we’ve seen, with the exception of Robotium, have done black box testing of the UI. However white box testing, whereby you test units internally, is just as important (there are a whole host of other tools that fill this role that we’ll cover in a future blog). For now, this is a primer on the types of tools available, what they are capable of,  and the roles that they fill. We hope we’ve provided a good launching point for you to get started.

The biggest thing to take away from the presentation and this blog is that you should make a habit of using some combination of these tools to test your apps. Develop a testing framework and stick to it. You are all programmers, so it should be no trouble for you to automate your tests. Don’t forget to test before every pull request!

Have any questions? Let us know in the comments.

Happy hacking,
Alex Bannerjee and Daniel Joyce

Leave a Reply