The article will discuss two functional testing tools for Android applications that come with the Android SDK, but are not very well known. Despite the very similar names, they are intended for several different purposes, and differ from other well-known tools, for example, from Robotium. Who cares - I ask under the cat.
Monkey
Let's start with the simplest tool - with Monkey. Monkey is not so much a functional testing tool as so-called. stress testing, or, as stated on the
official project documentation page - UI / Application Exerciser. Roughly speaking, Monkey emulates a phone with a running application in the paws of a monkey (well, or in the hands of a small child), followed by chaotic actions of the “user”. However, Monkey allows you to quite flexibly adjust “randomness”, the interval between events, their type, etc. For such testing, the source code of the application is not required - it just has to be installed on the device or an emulator, and the launch in the simplest case is as follows from the console:
$ adb shell monkey -p org.monkeys -v 500
Specify the package name of your application and the number of events generated. In case of an exception (Exception), the corresponding stack trace will be displayed in the console:
:Sending Pointer ACTION_MOVE x=-4.0 y=2.0 :Sending Pointer ACTION_UP x=0.0 y=0.0 // CRASH: org.monkeys (pid 325) // Short Msg: java.lang.NoSuchMethodException // Long Msg: java.lang.NoSuchMethodException: onButtonClick ... // java.lang.IllegalStateException: Could not find a method onButtonClick(View) in the activity class org.monkeys.MonkeysTestActivity for onClick handler on view class android.widget.Button // at android.view.View$1.onClick(View.java:2059) // at android.view.View.performClick(View.java:2408) ... ** Monkey aborted due to error. Events injected: 17 :Dropped: keys=0 pointers=0 trackballs=0 flips=0 ## Network stats: elapsed time=1479ms (1479ms mobile, 0ms wifi, 0ms not connected) ** System appears to have crashed at event 17 of 500 using seed 0
')
That's all about Monkey - test your application, and perhaps it will make it more reliable - who knows.
Monkeyrunner
Despite the name Monkey is similar to, MonkeyRunner is a completely different tool that allows you to perform functional testing of an application (“clicking” tests), providing an
API to control the device . MonkeyRunner is lower level than Robotium, and does not require application source code as compared to Robolectric. But only by testing the MonkeyRunner application is not limited to - it can be used to build systems that control android devices via the UI (and not only). MonkeyRunner uses Jython and test scripts or scripts can be written in Python, or recorded by a user using a recorder.
As already mentioned, MonkeyRunner is delivered together as part of the SDK, and the interpreter and its interactive console can be run using the
monkeyrunner
script located in the tools SDK directory.
Record and play the script
From the SDK we will need two python scripts: for the
recorder and the
player . They must be run accordingly with the help of
monkeyrunner
:
monkeyrunner ~/Projects/MonkeysTest/monkeyrunner/monkey_recorder.py
The recorder looks like this:

Using the “Export actions” button, the script can be written to a file, after which it can be played by the player:
monkeyrunner ~/Projects/MonkeysTest/monkeyrunner/monkey_playback.py /tmp/actions.out
Of course, the emulator should be started with all the above actions.
As you can see from the screenshot, MonkeyRunner operates with coordinates to select a control, which of course is not as convenient as choosing an element in Robotium or Robolectric and will depend on screen size, but it may be that this behavior will be required to create application testing scripts, not possessing familiar interface elements - for example, games.
Script writing
MonkeyRunner allows you to describe your own scripts using Python. In addition to managing the interface, you can remove and install the application, save screenshots, and more. An example of such a script:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice device = MonkeyRunner.waitForConnection() device.removePackage('org.monkeys') device.installPackage('/tmp/monkeys.apk') package = 'org.monkeys' activity = 'org.monkeys.MonkeysTestActivity' runComponent = package + '/' + activity device.startActivity(component=runComponent) MonkeyRunner.sleep(2.0) device.touch(50, 140, 'DOWN_AND_UP') device.type("Hello!") MonkeyRunner.sleep(1.0) device.takeSnapshot().writeToFile('/tmp/monkeyshot.png','png')
For more information about the features provided by the API, please refer to the official MonkeyRunner documentation.
Integration with Eclipse
To be able to run your scripts from Eclipse, it’s enough to do the following:
- Install PyDev extension
- In the PyDev settings, add a new Python interpreter as the “Interpreter Executable” specifying the
monkeyrunner
script (add exactly as Python, not Jython). For older versions of the SDK, you may need a wrapper script . - Add script scripts in their “Run configuration” specify for them just added interpreter.
You can of course create a separate Python project for scripts.
That's all that I wanted to talk about in brief. Perhaps the described testing tools will be useful to you.