认识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:62001
connected to 127.0.0.1:62001C:\Users\Administrator>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>

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 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

使用Monkey对apk做稳定性测试相关推荐

  1. Android测试能不能用monk,使用Monkey对apk做稳定性测试

    认识Monkey 官方文档:http://www.android-doc.com/tools/help/monkey.html 什么是Monkey? Monkey是Android中的一个命令行工具,可 ...

  2. android驱动测试,Android: 通过 cucumber 驱动 monkey 做稳定性测试

    主要内容 稳定性测试是什么 Monkey 介绍 自动化 Monkey 稳定性测试是什么 通过随机点击屏幕一段时间,看看 app 会不会奔溃,能不能维持正常运行. Monkey 介绍 Monkey 是一 ...

  3. monkey Android稳定性测试

    monkey测试原理: Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进 ...

  4. 稳定性测试怎么做,这篇文章彻底讲透了

    稳定性对产品的重要性不言而喻. 而作为质量保障,在稳定性测试方面的探索也在不断演化.记得两年前我们做稳定性测试还是基于恒定的压力,7*24小时长时间运行,关注的指标无非是吞吐量TPS的抖动.响应时间的 ...

  5. android稳定性测试

    1.monkey介绍 说到稳定性测试我们就不得不说monkey,monkey就是猴子,所谓的稳定性测试,就像一只猴子,在设备上进行随机点击触摸等事件操作以此来发现软件的异常,它的主要目的就是为了测试a ...

  6. 什么是App稳定性测试?什么是App弱网测试?

    App稳定性测试? 由于app软件是安装在手机的操作系统之上的,为了保证app能够在长时间内运行操作不会出问题,所以需要做稳定性测试,稳定性测试需要用到monkey工具,monkey工具是基于命令行的 ...

  7. LTP--linux稳定性测试 linux性能测试 ltp压力测试

    说明:在写这篇文章之前,本人也不曾了解LTP是干嘛的,直到参加一次技术沙龙才了解到它是用来对linux系统进行稳定性测试的一个开源工具,演讲人是世纪佳缘运维部门的技术老总!平时我们这些做运维朋友们都很 ...

  8. 性能测试,压力测试, 负载测试,稳定性测试的定义和区别

    性能测试压力变化模型 随着单位时间流量的不断增长,被测系统的压力不断增大,服务器资源会不断被消耗, TPS 值会因为这些因素而发生变化,而且符合一定的规律.淘宝网性能测试压力变化模型如 图中: a 点 ...

  9. jmeter稳定性测试

    jmeter稳定性测试 参数解释 做稳定性测试前先要了线程组中的几个参数 1.Number of Threads(users):并发用户数 2.Ramp-up period(seconds):每隔几秒 ...

最新文章

  1. gulp项目配置指南
  2. Unity3D for iOS初级教程:Part 2/3
  3. MATLAB学习笔记(七)
  4. linux驱动编写(电源管理驱动)
  5. UVA11161 Help My Brother (II)【大数+递推】
  6. C# 浏览器控件 谷歌、火狐内核
  7. 借用implicit创建我们自己的布尔数据类型-create MyBool data type by implicit
  8. html渐变颜色代码表,渐变颜色代码表
  9. 诛仙2怎样修改服务器时间同步,《诛仙2》2月22日更新公告
  10. php五行万年历,PHP制作万年历
  11. 台式计算机如何升级,电脑硬件如何升级:PC硬件升级不盲目 对症下药最靠谱
  12. 学霸题 - 数正方形
  13. SQL基础教程学习第六站:数据更新
  14. 领导提拔你,还是干掉你,从来都不是看能力!
  15. 几种优化算法(求最优解)
  16. 不得不知的Web知识 —— HttpClient中SocketTimeOut、ConnectionTimeOut与ConnectionManagerTimeOut区别
  17. 大二期末作孽(SpringBoot+Vue前后端分离博客社区(重构White Hole))
  18. 智慧城市2020的三个焦点:网格化、健康码、老年人
  19. 一问三不知之log4j2漏洞简析
  20. Windows安装Weblogic

热门文章

  1. 如何留住优秀的测试人员
  2. 可以参悟人生的十个故事
  3. 充电速度公式_充电电池充电时间计算方法
  4. 《Effective Java》读书笔记,flutter游戏开发
  5. Java夜未眠.程序员的心声
  6. 做网络必须掌握83句话
  7. 【JS面试题】面试官问我:遍历一个数组用 for 和 forEach 哪个更快?
  8. 资本寒冬,经济下行,中小企业如何“御寒”
  9. 北京化工大学计算机调剂要求,2020年北京化工大学考研调剂信息
  10. 关于vlc播放器的ActiveX控件调用问题