使用Monkey对apk做稳定性测试

认识Monkey

官方文档:https://developer.android.com/studio/test/monkey.html

什么是Monkey?

Monkey是Android中的一个命令行工具,可以运行在模拟器或实际设备中,它向系统发送随机的用户事件流(按键输入,触摸屏输入,手势输入等),实现对应用程序随机,快速疯狂操作的效果。Monkey测试是一种为了测试软件的稳定性,健壮性的快速有效的方法。

 Monkey测试的目的是什么?

  • Android应用的Crash
  • Android应用的ANR

一、安卓adb环境搭建

略,android开发环境:使用Android Studio搭建Android集成开发环境(图文教程)

二、Monkey使用步骤

1.ADB连接Android测试设备,在命令行输入 adb devices 查看设备连接情况

如果是真机,直接使用USB线连接真机和PC机,adb devices即可看到真机已经连接上了,如

C:UsersAdministrator>adb devices
List of devices attached
611AKBPP22HR5   device

如果使用模拟器,如夜神模拟器,则需要手工连接才可以看到模拟器

C:UsersAdministrator>adb connect 127.0.0.1:62001
connected to 127.0.0.1:62001

C:UsersAdministrator>adb devices
List of devices attached
127.0.0.1:62001 device

2.确定待测试应用的PackageName

方法一:直接问开发即可

方法二:使用一些查包工具可以获得PacakgeName

方法三:通过重签名软件可以获得,参照《使用re-sign.jar对apk进行重签名

3.使用Monkey发一条测试命令

示例:

adb shell monkey -p com.ssjj.recorder -v 50

包名:com.ssjj.recorder

-p 指定测试包名

-v 显示默认程度的信息

事件数设定为50

三、Monkey 命令常用参数详解

Monkey基础语法

$ adb shell monkey [options] <event-count>
CategoryOptionDescription
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. See Traceview 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.

 Monkey组合命令举例

adb shell monkey -p com.ssjj.recorder --pct-motion 25 --pct-touch 40 --pct-nav 10 --pct-appswitch 10 --ignore-crashes --ignore-timeouts --throttle 300 -s 200 -v -v -v 1000

命令是需要组合的

命令本身是需要根据不同的测试切入点来进行编写:

1.触摸事件和手势事件是用户最常见的操作,所以通过 --pct-touch 和 --pct-motion 将这两个事件的占比调整到40%与25%

2.目标应用包含了多个Activity,为了能覆盖大部分的Activity,所以通过--pct-appswitch将Activity切换的事件调整到10%

3.使用--ignore-crash 和 --ignore-timeouts 参数使Monkey遇到意外是能继续执行

4.使用--throttle参数来控制Monkey每个操作之间的时间间隔

四、Monkey的报告

  • 主要关注CRASH和ANR
  • Monkey并没有完整的报告,可以重定向monkey日志到文本文档
  • 编写脚本正则匹配报告中的关键字

异常日志分析

1.找到monkey里面的哪个地方出差
2.查看Monkey里面出错前的一些事件动作,并手动执行该动作
3.若以上步骤不能找出,可以使用之前执行的monkey命令再执行一次,注意seed值要一样。
4.注意第一个switch以及其他异常信息

常见问题
1.ANR问题:在日志中搜索“ANR”
2.崩溃问题:在日志中搜索"CRASH"

假如运行结束后,结果显示为:
** System appears to have crashed at event 48 of 50 using seed 1499432070709
则说明系统在50个操作的第48个操作里面出现了异常,用的种子是1499432070709。我们下次用相同的操作序列再现这个异常,如
adb shell monkey -p com.ssjj.recorder -s 1499432070709 500


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
原文地址:https://www.cnblogs.com/guanfuchang/p/7132599.html