How to stress test your Android app with Monkey

Today I want to show you a small nice tool, to stress test your Android app. The tool is called monkey and is part of the Android SDK. The tool is not new, but I didn’t had the chance/ time to work with the tool.

Monkey is a program that runs on your device or emulator. While running it is generating pseudo-random user events such as touch, click, rotate, swipe, mute the phone, shutdown wifi and many more, to stress test your app and to see how your app is handling all those inputs.
To execute the monkey stress test you need the package name of your apk file you want to test. If you have access to the app code, you find the package name in the AndroidManifest.xml. If you have just the compiled apk file, you can use the tool Android Asset Packaging Tool (aapt) to get the package name from the file. aapt is located in the build-tools folder of the installed Android SDK version on your machine. For example, if you have the latest version of Android (4.4) installed, the path of aapt is:

Users/dknott/Documents/android/sdk/build-tools/android-4.4

If you start aapt with ./aapt you get an overview of possible parameters and options to use.aapt_help

To get the package name of the apk file, you need to execute the following command:

./aapt d badging /Users/dknott/Downloads/ExampleTestProject_v5.1/NotePad/bin/NotePad.apk | grep 'pack'

The output is:

package: name='com.example.android.notepad' versionCode='' versionName=''

With the package name, navigate to the platform-tools directory where adb is located and execute the command:

./adb shell monkey -p com.example.android.notepad -v 2000

The 2000 is the number of random commands that monkey will perform on the app.
See the following youtube video, where monkey is testing the Notepad app provided by the Robotium project.

At the end of the video you can see, that the Notepad app is crashing. The following stacktrace shows the error. It seems that the Notepad app has a problem in the onResume Method.

E/AndroidRuntime﹕ FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to resume activity {com.example.android.notepad/com.example.android.notepad.NoteEditor}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:176)
 at android.app.ActivityThread.main(ActivityThread.java:5419)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
 at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
 at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
 at android.database.CursorWrapper.getString(CursorWrapper.java:114)
 at com.example.android.notepad.NoteEditor.onResume(NoteEditor.java:180)
 at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
 at android.app.Activity.performResume(Activity.java:5450)
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2909)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:176)
 at android.app.ActivityThread.main(ActivityThread.java:5419)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
 at dalvik.system.NativeStart.main(Native Method)

If you are testing an Android app you should try monkey, it is really easy to use and the results are often really interesting :).

Have fun!

By Daniel Knott

Advertisements

9 thoughts on “How to stress test your Android app with Monkey

  1. Hey Daniel,
    thanks for the article. If you want to try stress testing on multiple devices you can also use TestObject (testobject.com).
    (Disclaimer I’m founder of TestObject; but stress-testing is for free)
    Regards,
    Hannes

  2. Pingback: Testing Bits – 3/30/14 – 4/5/14 | Testing Curator Blog

  3. Pingback: How to stress test your iOS app | Adventures in QA

  4. Hi Daniel,
    Using Monkey to stress test android app is very much useful and easy to and help in reducing little efforts for testers .

    Thanks,
    Lalit Sharma

  5. Pingback: How to stress test your Android app with Monkey | Adventures in QA - appgong

  6. Hey daniel can you please mail me the steps from very starting as I am in learning phase and very much confused about the steps.

  7. Hi Amit,
    if you have an app that you want to test you just need to execute the following command to use monkey :

    ./adb shell monkey -p com.example.android.notepad -v 2000

    All you need to do is to get the package name of the starting activity and replace it in the mentioned command.

    Greets,
    Daniel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s