Android's Monkey Test

Source: Internet
Author: User

This article is also published in my personal website www.yaoxiaowen.com

Monkey testing is one of the simplest tools for Android automated testing. Although simple, it is useful to test the app's robustness and reduce crashes. So it is recommended that the app can often do some monkey testing, after work, open the monkey test, do not have the tube, the next day to see the result is good.

One: Monkey Introduction
Monkey is a command-line tool for Android that runs on your phone. (Can simulator can be real machine), it to the system by sending pseudo-random user event stream (such as gestures, keys, touch screen touch events, etc.), to achieve the test of the app. Of course, because its event stream data is random and cannot be customized, this is a flaw.
So the name of the monkey is quite appropriate, just like a monkey, a little messy,

Second, the use of pre-preparation
1, we usually in the CMD command tool to use the ADB command, etc., to enter the ADB's file directory, this is more troublesome, so we have to add the ADB command into the environment variable window. The method is as follows (Win7 for example):
Click: System variables, environment variable, advanced system settings, properties, computers, and so on. Add a line after the value of the variable. \android\sdk\platform-tools, (of course this is my own computer directory, other people's computer directory may be different).
So the Platform-tools folder under the Adb,sqlite3,fastboot and other tools can be used directly.
2, the phone opens the developer mode inside the USB debugging, and then the notification Bar option to choose to transfer files, and not choose to only charge,
USB connection to the real machine (or turn on the simulator), use the command
ADB devices
Display content:
List of devices attached
ec771855 Device
This means that the ADB is already connected to the phone.

Three, run monkey

cmd command line Input command.
$ adb shell monkey-s 161218-p com.tc.tickets.train.dev-v-v–throttle 60000 > C:\Users\Administrator\Desktop\ton Gcheng\log\moneky_12121802.txt

Some of the option's explanations:
adb shell Monkey: Boot Monkey,monkey is actually launched through the shell of the Android environment.
The seed value of the-s pseudo-random number generator. The same seed value generates the same sequence of events, so the value is changed to be different before each run, and the date of the day is the simplest to remember.
-P needs to test the app package name, constraint restrictions, but can actually specify multiple packages, such as: adb shell monkey-p com.yaoxiaowen.prj–p Com.yaoxiaowen.demo
-V
Used to specify the level of feedback information (the level of information is the verbosity of the log), with a total of 3 levels, respectively, with the corresponding parameters as shown in the following table:
Log levels: Level 0
Example: –v
Description: A default value that provides only a small amount of information, such as startup hints, test completion, and final results

Log levels: Level 1
Example: –v-v
Description: Provides a more detailed log, including each event message sent to the activity

Log levels: Level 2
Example: –v-v-V
Description: The most detailed log, including the selected/unchecked activity information in the test

–throttle (MS)
Inserts a fixed delay between events. This option slows down the execution speed of the Monkey. If this option is not specified, the Monkey will not be delayed and the event will be produced as soon as possible. So I suggest to add a certain delay, because the average person's finger click speed, it is not possible to follow the machine's click speed, so I think, if it is because the click too fast caused by the bug, then can be ignored.

60000
This command represents a total of 60,000 run event streams. Before setting this value, remember to combine the value of –throttle to calculate the time you want it to run (usually I set it to run for one night and then end, the next day to work, just to see the results).

> C:\Users\Administrator\Desktop\tongcheng\log\moneky_12121802.txt
This command means, let it output log information into the desktop of the Moneky_12121802.txt file, if not set this sentence, log information is output in the cmd command line, not easy to view, so it is still input into the file to see it well.

Of course, there are many other commands, but these simple commands are enough for us to test and use.

Four: Monkey stop condition
If the application crashes, or if there are other runaway exceptions, or the app does not respond (ANR). Monkey will stop and make an error. Otherwise it will execute the number of times you set. (for example, 60,000 times) and will not stop until the execution is complete.

Five: How to stop monkey in the middle
Monkey has a very maddening feature, is that once run up, even if you put the USB cable and the PC disconnected, monkey script will be calm execution. So if we want to stop halfway, then there's a way.

1. ADB shell
2. Top | grep monkey (or PS | grep Monkey also OK)
Shown below:
5447 0 1% S 262960K 10328K root Com.android.commands.monkey
5447 0 0% S 262960K 10324K root Com.android.commands.monkey
3, find the process ID 5447, and then kill it again OK
Kill-9 5447

Six: Monkey test results analysis
To get the log file of monkey, we use the keyword to determine whether the test is normal.
1, if the last line of content is, Monkey finished, then it indicates that the test process, no exception occurred.
eg
# # Network stats:elapsed time=3799ms (3799ms Mobile, 0ms WiFi, 0ms not connected)
Monkey finished
2, indicating the presence of the exception keyword: CRASH, not responding, anr,exception,out of Memory,error
If these keywords are present, it indicates that the test has an exception, we need to combine the context of the log to determine the cause of the exception, and then modify the code.

3, abnormal end, there is no corresponding exception keyword.
At this time, it is necessary to look at the last few lines of the log file is what content, the corresponding analysis, if the analysis does not come out of what is abnormal, it is the external reason rather than the procedure itself. such as power off, memory shortage and so on.

Seven: Monkey log automatic analysis script
For monkey analysis, manual search is too cumbersome, so having scripts to handle is the best. I found a bat script on http://www.cnblogs.com/findyou/p/4106285.html this blog (Thanks again for sharing), (but the code part of his blog was copied directly to the bat file, and there was an error, and it took me a long time. , presumably because of the coding problem), I took this bat script and made a little modification. Put on the CSDN (http://download.csdn.net/detail/yaowen369/9759769), you can download it directly.
How to use: Directly monkey the TXT file of the log, drag the mouse to this bat file, the output is executed.

VIII: Other ISSUES
A problem has plagued me for a long time, because the app code has some H5 pages, MONKEY test may sometimes go into these HTML pages, into easy, out of the difficult, and can not find a better solution, only in the code to add a monkey_test boolea value switch, Then, when entering the button key event on the H5 page, the value is opened when the monkey test is performed, and a special apk is compiled to prevent it from jumping into the H5 page.

I hope this blog can help to the use of Monkey test counterparts, the text of any deficiencies or errors in the place, welcome criticism.

Android's Monkey Test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.