Monkey是另一个测试工具,是一个命令行工具,可以运行在设备或模拟器器里。向系统发送伪随机的用户事件流(键盘输入、触摸屏输入、手势输入等),实现对应用程序的压力测试,是测试软件的稳定性、健壮性的快速有效的方法。
Monkey的特征
1、测试的对象仅为应用程序包,有一定的局限性。
2、 Monky测试使用的事件流数据流是随机的,不能进行自定义。
3、可对MonkeyTest的对象,事件数量,类型,频率等进行设置。
Monkey Test执行过程中在下列三种情况下会自动停止:
1、如果限定了Monkey运行在一个或几个特定的包上,那么它会监测试图转到其它包的操作,并对其进行阻止。
2、如果应用程序崩溃或接收到任何失控异常,Monkey将停止并报错。
3、如果应用程序产生了应用程序不响应(ANR)的错误,Monkey将会停止并报错。
通过多次并且不同设定下的Monkey测试才算它是一个稳定性足够的程序。
在通过adb shell进入命令行,在命令行运行monkey进行测试。
monkey是运行在/system/bin/monkey,这是个批处理文件,
内容如下:
base=/system
export CLASSPATH=$base/framework/monkey.jar
exec app_process $base/bin com.android.commands.monkey.Monkey $*
可以看出其实现实在com.android.commands.monkey.Monkey里,如果要定制自己的Monkey工具,则需要修改这个包。
下面举个示例,它启动计算器,并向其发送500个伪随机事件.
#monkey -p com.android.calculator2 -v 500
当要测试多个包的时候,可以加多个-p参数
#monkey -p com.android.calculator2 -p com.android.browser -v 500
不同的APK有不同的侧重点,或许需要调整随机事件中不同事件的比例。
--pct就是这个用处。比如:" --pct-touch 80% " 表示生成的随机事件中触摸事件占有80%,而” --pct-syskeys 5%”"则表示生成的随机事件中按键事件占5%。
详细的参数如下
Category | Option | Description |
General |
| 帮助 |
| Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing. | |
Events |
| Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events. |
| Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible. | |
| Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.) | |
| Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.) | |
| Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.) | |
| Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.) | |
| Adjust percentage of "major" navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.) | |
| Adjust percentage of "system" key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.) | |
| Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package. | |
| Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth. | |
Constraints |
| If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you'll need to specify those packages as well. If you don't specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package. |
| If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category. | |
Debugging |
| When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application. |
| If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files. | |
| Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
| Normally, the Monkey will stop when the application experiences any type of timeout error such as a "Application Not Responding" dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
| Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
| Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event. | |
| Watches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop. | |
| Stops the Monkey from executing until a debugger is attached to it. |
Category | Option | Description |
---|---|---|
General | --help | Prints a simple usage guide. |
-v | Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing. | |
Events | -s <seed> | Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events. |
--throttle <milliseconds> | Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible. | |
--pct-touch <percent> | Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.) | |
--pct-motion <percent> | Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.) | |
--pct-trackball <percent> | Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.) | |
--pct-nav <percent> | Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.) | |
--pct-majornav <percent> | Adjust percentage of "major" navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.) | |
--pct-syskeys <percent> | Adjust percentage of "system" key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.) | |
--pct-appswitch <percent> | Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package. | |
--pct-anyevent <percent> | Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth. | |
Constraints | -p <allowed-package-name> | If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you'll need to specify those packages as well. If you don't specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package. |
-c <main-category> | If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category. | |
Debugging | --dbg-no-events | When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application. |
--hprof | If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. SeeTraceview for more information on trace files. | |
--ignore-crashes | Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
--ignore-timeouts | Normally, the Monkey will stop when the application experiences any type of timeout error such as a "Application Not Responding" dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
--ignore-security-exceptions | Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. | |
--kill-process-after-error | Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event. | |
--monitor-native-crashes | Watches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop. | |
--wait-dbg | Stops the Monkey from executing until a debugger is attached to it. |