Sunday, July 22, 2012

Monkey test on Android application.

You may be wondering about the test name or thinking that it may be a misnomer. Well, this is not a misnomer. It is a kind of stress test done on an application.

Monkey Test:

Monkey test is a kind of Unit test that runs with no specific test case in mind. Test cases are randomly selected and executed. For example, giving random string to text-field or clicking some random buttons of the application. These kind of test cases check the stability of the application. Monkey test is a kind of black box test.
In my career, I have seen such kind of tests in telecommunication domain (handset manufacturer). In such test cases, the tester can do any sort of random things on the phone and check whether the phone application can handle such scenario or not. In real world no one is going to educate people about how to use the phone. They use the phone in N number of ways and expect it to work under all circumstances. 

Android Monkey test:

If you are an Android application developer and want to do Monkey test on the application, then you can use Android Monkey command line application. It generates a stream of random pseudo user events such as button click, touch, gesture etc. This application can be run on your device or emulator. Monkey test can be used to do stress test on the application.

Perquisite: Android SDK should be present and Android Debug Bridget(ADB) should be accessible. 

During the test if an exception occurs or the application crashes, then Monkey will stop the execution or you can even instruct Monkey to continue the test after exception. It also provides certain debugging configurations where you can configure monkey to get the test execution log.
You may be wondering if Monkey randomly generates the events,  then is it possible to create the same test scenario? I would say yes, Monkey takes care of that. If you give the same seed value (-s option) then Monkey creates same sequence of events. This feature enables you to reproduce the same scenario and allows to take the log and analayze the issue.

Running Monkey test on Android application:

#basic command format:
$ adb shell monkey [options] <event-count>
#To get command argument information, run the following command.
$ adb shell monkey --help
#The command below exercises monkey test on given package(your.package.name here)
# It enables the verbosity and fires 500 random events
$ adb shell monkey -p your.package.name -v 500 
#If you want to increase the verbosity, then increase the number of v like shown below.
#The default verbosity is Level 0 which shows minimum information, 
#i.e startup notification, test completion, and final results
$ adb shell monkey -p your.package.name -vvv 500 
#you can also target activity with certain category
#e.g android.intent.category.LAUNCHER
$ adb shell moneky -p your.package.name -c android.intent.category.LAUNCHER -v 500
#The throttle option enables you to keep delay between events.
#The command below fires events after 3 seconds and generates 50 events.
$adb shell monkey -p your.package.name -v -throttle 3000 50 

you can know more about Application Exerciser Monkey here

Monkey, sometimes, creates problem with adb server. If you notice that adb server is not behaving properly, then you may have to restart the server. Use the following command to do so.
adb kill-server
adb start-server 


Related topics:
Android tutorials
How to do stuff in Android?

Your valuable comments are always welcomed. It will help to improve my post and understanding.

"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest." 
By : Confucius