Android: Testing UI Using Application Exerciser Monkey šŸµ

Umar Saidu Auna
5 min readOct 11, 2021

--

Photo by freestocks on Unsplash

Itā€™s surprising that few developers actually test their applications. ā€œRelease it now, fix it laterā€ is a common practice these days. A buggy app will always cost the developer and the company its reputation. As a developer myself, I can understand the tight deadline challenges. Keeping all these things in mind, Google has introduced the Monkey tool, which takes care of crashes and ANR(Application not Responding).

What is Monkey? šŸµ

Before I begin, you all know how monkeys šŸ’ in the jungle or zoo jump from one tree to another in search of food, thatā€™s exactly whatā€™s happening here, in this case, you will run a command in your terminal and it will randomly click views in your app screen (Buttons, TextView etc) until the app crash or the number of clicks declared is exceeded. I guess thatā€™s where the name came from, who knows šŸ¤·šŸ¼ā€ā™‚ šŸ¤·šŸ¼ā€ā™‚ šŸ¤·šŸ¼ā€ā™‚

The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.

When the Monkey runs, it generates events and sends them to the system. It also watches the system under test and looks for three conditions, which it treats specially:

  • If you have constrained the Monkey to run in one or more specific packages, it watches for attempts to navigate to any other packages and blocks them.
  • If your application crashes or receives any sort of exception, the Monkey will stop and report the error.
  • If your application generates an application not responding error, the Monkey will stop and report the error.

Why Monkey? šŸµ

Usually, in the release cycle, we perform QA testing and before we do that, we do Developer testing as well; but we are able to perform regression testing as well as Unit Tests at their end that takes as less as 5 sec. It helps catch frequent exceptions like NullPointer Exception, unhandled exception, ANR (Application not responding), etc. faster and easier. It also handles system-level events like background application performance and parallels running applications in a better manner.

How Monkey Works? šŸµ

Note: This tool is not used for automation testing. This only helps to find the crashes that may not appear during regular use of the application.

Let's get started:

Step 1:

You can launch the Monkey using a command line on your terminal. Because the Monkey runs in the emulator/device environment, you must launch it from a shell in that environment.

Run the above command by providing your package name i.e adb shell monkey -p com.auna.test -v 500

-v: verbose method

500: Number of random events which you want to generate at a time.

-p: App package name example mine is ā€œcom.auna.testā€

You can check here for more commands https://developer.android.com/studio/test/monkey#command-options-reference

Step 2:

Demo of Testing UI Using Application Exerciser Monkey

When you run this test, you can watch your device or emulator. You will notice random events on UI elements are being generated.

When the application crashes, it will stop the exerciser and prints the report on the terminal window. The following screenshot depicts a crash report while testing my application.

Step 3:

Since the tool generates a very random event and so it plays with system UI as well like toggle Wifi/Bluetooth/Flight mode state and so on. But in case if you only want to test inside the application only, for device user 5.0(Lollipop) and above you have the Screen Pinning feature.

Using screen pinning you can pin(lock) the application. To enable this follow these steps:

open this feature in device ā€œsettingsā€ > ā€œsecurityā€ > ā€œscreen pinningā€ and then toggle ā€œOnā€ and ā€œAsk for PIN before unpinningā€. Go back home and swipe up to view the Application you want to test, the icon will appear at the top click on it and a drop-down menu appear you will see ā€œApp infoā€, ā€œSplit Screenā€ and ā€œPinā€ select Pin and select GOT IT to pin the Application you want to test.

Using the same instructions will generate events inside the application.

To remove it you just need to hold back and home icon for a while and the system will release the pinning way.

Note Monkey šŸµ and MonkeyRunner šŸ’ are two different tools. In monkeyrunner, you can have access to the device and application component while monkey itā€™s completely automatic. Monkeyrunner is used by QA to perform automation and can be useful in a different way includes UI comparison and all. Monkey simply give a very quick idea if any exception can be got by back and forth.

Conclusion

There are so many unit testing tools like Junit, Monkey, Espresso, Mockito, Robolectric etc. for different purposes. Out of all these tools, Monkey is the easiest to use with lots of features are as follows:

  • If your app crashes or has any sort of unhandled exception, it will stop and report the error.
  • If there is any ANR, it will report.
  • You can restrict it to any specific package.

Thanks for reading this article. Be sure to clap and recommend this article if you found it helpful. It means a lot to me.

Chat me up on Twitter or Linkedin

References

--

--

Umar Saidu Auna

Kotlin Evangelist and Flutter Warrior | Tech Community Organizer, GDG Minna | Volunteer @nrcs_ng