使用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:\Users\Administrator>adb devices
List of devices attached
611AKBPP22HR5 device
如果使用模拟器,如夜神模拟器,则需要手工连接才可以看到模拟器
C:\Users\Administrator>adb connect 127.0.0.1:
connected to 127.0.0.1: C:\Users\Administrator>adb devices
List of devices attached
127.0.0.1: device
2.确定待测试应用的PackageName
方法一:直接问开发即可
方法二:使用一些查包工具可以获得PacakgeName
方法三:通过重签名软件可以获得,参照《使用re-sign.jar对apk进行重签名》
3.使用Monkey发一条测试命令
示例:
adb shell monkey -p com.ssjj.recorder -v
包名:com.ssjj.recorder
-p 指定测试包名
-v 显示默认程度的信息
事件数设定为50
三、Monkey 命令常用参数详解
Monkey基础语法
$ adb shell monkey [options] <event-count>
| 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. 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 --pct-touch --pct-nav --pct-appswitch 10 --ignore-crashes --ignore-timeouts --throttle -s -v -v -v
命令是需要组合的
命令本身是需要根据不同的测试切入点来进行编写:
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测试开发圈”,了解更多测试教程!***
使用Monkey对apk做稳定性测试的更多相关文章
- [转贴]LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---IBM 的 linux test project
https://blog.csdn.net/melody157398/article/details/24354415 LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---I ...
- LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---IBM 的 linux test project
LTP--linux稳定性测试 linux性能测试 ltp压力测试 ---IBM 的 linux test project Peter盼 2014-04-23 11:25:49 20302 收藏 ...
- LTP--linux稳定性测试 linux性能测试 ltp压力测试 内核更新 稳定性测试
LTP--linux稳定性测试 linux性能测试 ltp压力测试 zhangzj1030关注14人评论33721人阅读2011-12-09 12:07:45 说明:在写这篇文章之前,本人也不曾了 ...
- LTP--linux稳定性测试 linux性能测试 ltp压力测试 ltp-pan
LTP--linux稳定性测试 linux性能测试 ltp压力测试 zhangzj1030关注14人评论33710人阅读2011-12-09 12:07:45 说明:在写这篇文章之前,本人也不曾了 ...
- Android客户端稳定性测试——Monkey
修改时间 修改内容 修改人 2016.6.20 创建 刘永志 2016.6.29 完成 刘永志 Monkey简介: Android SDK自带的命令行测试工具,向设备发送伪随机事件流,对应用程序进行进 ...
- monkey稳定性测试的步骤及策略
1.adb的作用是什么?adb的全称:android debug bridge 安卓调试桥梁,包含在 Android SDK 平台工具软件包中.通过该命令与设备进行通信,以便进行调试adb可以同时管理 ...
- 【Monkey】Monkey稳定性测试常用命令
Monkey稳定性测试常用命令: 1.adb shell monkey n 2.adb shell monkey -p com.android.calculator2 1000 3.adb shel ...
- Monkey Android app稳定性测试工具之Monkey使用教程
Monkey Android app稳定性测试工具之Monkey使用教程 by:授客 QQ:1033553122 由于篇幅问题,仅提供百度网盘下载链接: Android app稳定性测试工具之Monk ...
- Monkey 稳定性测试
学习网址: https://blog.csdn.net/lucytan01/article/details/79958727 https://blog.csdn.net/hebbely/article ...
随机推荐
- plotly简单绘制柱状图
代码: import plotly.offline as pltoff import plotly.graph_objs as go def bar_charts(name="bar_cha ...
- socketserver模块、MySQL(数据库、数据表的操作)
一.socketserver实现并发 基于tcp的套接字,关键就是两个循环,一个链接循环,一个通信循环. socketserver模块中分两大类:server类(解决链接问题)和request类(解决 ...
- sql server always on 2014安装配置
SQL 2014 AlwaysOn 搭建 原文:SQL 2014 AlwaysOn 搭建 AlwaysOn底层依然采用Windows 故障转移群集的机制进行监测和转移,因此也需要先建立Window ...
- Linux环境安装nodejs
安装node 去官网下载nodejs 根据下载的文件可以看出它的压缩方式是.xz的方式,所以不能直接使用linux命令tar直接下载. xz -d node-v10.6.0-linux-x64.tar ...
- jsp创建cookie
<jsp:include flush="true" page="header.jsp" /> <script type="text/ ...
- 吴超老师课程--Hive的执行语句
为什么选择Hive? (1)基于Hadoop的大数据的计算/扩展能力(2)支持SQL like查询语言(3)统一的元数据管理(4)简单编程 一:Hive的数据类型(1)基本数据类型tinyint/sm ...
- httpmessageconverter requestbody responsebody
@ResponseBody @RequestMapping("/testHttpMessageConverter") public String testHttpMessageCo ...
- cdoj1338郭大侠与英雄学院
地址:http://acm.uestc.edu.cn/#/problem/show/1338 思路: 郭大侠与英雄学院 Time Limit: 6000/2000MS (Java/Others) ...
- iconfont的使用
首先你要有一个图标库的账号,我们使用的是阿里矢量图标库,其次你要有一套已经设计好的图标原图.如果你具备了这些,就可以和我一起看iconfont的使用姿势了. 写在前面 不结合其他矢量库或UI框架一起使 ...
- EasyUI:获取某个dategrid的所有行数据
EasyUI:获取某个dategrid的所有行数据 var rows = $("#grid").datagrid("getRows"); for(var i=0 ...