使用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 ...
随机推荐
- 白话Redis分布式锁
redis分布式 简单来说就是,操作redis实例时,不是常规(单机)操作一个实例,而是操作两台或多台,也就是基于分布式集群: 而且redis内部是单进程.单线程,是数据安全的(只有自己的线程在操作数 ...
- SSD(Single Shot MultiBox Detector)二读paper
SSD KeyWords:Real-time Object Detection; Convolutional Neural Network Introduction 目前最尖端(State-of-ar ...
- J2EE之JPA
POJO(plain old java object普通java类):具有setter/getter方法的Java类就称为POJO POJO转化为实体,手工使用标记, @Entity public c ...
- 运行hadoop自带的wordcount例子程序
1.准备文件 [root@master ~]# cat input.txt hello java hello python hello c hello java hello js hello html ...
- Eclipse插件--一次copy多个文件的相对路径路径
因为工作需要, 在网上找了很多插件, 本来有个easyTools应该可以, 但下载文件好像没了. 只好自己动手搞了一个简单的, 暂时一个复制文件相对路径的功能, 有同样需求的童鞋, 可以试一下 plu ...
- 《深度学习》Textbook第十章学习笔记
深度学习 第10章 序列建模:循环和递归网络 1.循环神经网络介绍 相比卷积神经网络:专门用于处理网格化的数据(如图像),可以很容易扩展到更具有很大宽度和高度的图像,以及处理大小可变的图像: 循环神经 ...
- APP数据埋点分类方式
1.数据埋点的重要性 在现实工作中,数据的整体流程为:数据生产-数据采集-数据处理-数据分析和挖掘-数据可视化,其中,数据采集是很重要的一个环节,数据采集得全不全.对不对,直接决定数据广度和质 ...
- appium入门基础
1. 建立session时常用命令: DesiredCapabilities cap = new DesiredCapabilities(); cap.SetCapability("brow ...
- iOS学习之七牛云存储应用
前言 七牛云存储,是专为移动时代开发者打造的数据管理平台,为互联网网站和移动App提供数据的在线托管.传输加速以及图片.音视频等富媒体的云处理服务. 七牛云官网http://www.qiniu.com ...
- ReportView 添加自动增长序号
在显示序号的列中输入相关的值: 在此表达式的后面是Nothing,不是null,不要搞错.