一、介绍
monkey 工具:是一个可以自动化测试安卓app的一个工具,要编写 monkey script,在monekey runner中运行,模拟人类的操作,完成app自动化测试,最终将app的crashes 日志交付开发进行app修改。
adb命令:实现手机上的monkey工具的控制与使用,同是可以使用adc在手机上完成安装与卸载。
Monkey Script:帮助完成重复的操作过程
MonkeyRunner: 提供三大API
二、2个背景问题
1.为什么开展压力测试
提高产品的稳定性;
提高产品的留存率。
2.什么时候开展压力测试
首轮功能测试通过后;
下班后的夜间进行。
三、7个基础知识
1.手工测试场景与自动测试场景
根据手工测试的理论分析,总结出自动化测试的中心问题是解决事件流的问题,而事件流是手机在处理问题的时候的操作或者说是运行过程。

2.Monkey测试工具介绍
1)什么是monket?
Monkey是发送伪随机用户事件的工具。
2)Monkey在哪?
在我们的手机系统里面。为了与之进行通信,我们需要借助使用ADB。
3)什么是ADB?
Android Debug Bridge 安卓调试桥,软连接adb,手机与电脑的连接

3.MonkeyScript测试命令集合介绍
1)什么是MonkeyScript?
MonkeyScript 是一组可以被Monkey识别的命令集合。
MinkeyScript 可以完成重复固定的操作。
2)什么是MonkeyRunner?
MonkeyRunner提供了一系列的API。
MonkeyRunner可以完成模拟事件及截图操作。
3)Monkey和MonkeyRunner区别?
Monkey:在adbshell中,生成用户或者系统的伪随机事件
MonkeyRunner:通过API定义特定命令和事件控制设备
4)MonkeyRunner APIS
MonkeyRunner:用来连接设备或者模拟器
MonkeyDevice:提供安装、卸载应用,发送模拟事件
MonkeyImage:完成图像保存及对比的操作。
5)MonkeyRunner的测试类型
多设备控制
功能测试
回归测试
4.异常测试结果
CRASH:即崩溃,应用程序在使用过程中,非正常退出
ANR:APPlication Not Responding

四、6个实践小案例
1.一次android的压测实践
准备测试环境:Android SDK环境、Python环境
下载Android SDK链接:www.android-studio.org
(Android SDK下载及配置教程链接:https://www.jianshu.com/p/cc99dc711802)
python下载链接:https://www.python.org/
(python下载安装及配置教程链接:https://www.jianshu.com/p/f166289755c3)
实践操作步骤:
步骤1:在手机开发者选项中,将USB调试选上。

步骤2:确认手机和电脑已经成功连接,adb devices 进行确认。

步骤3:安装测试APP 命令:adb install package.apk。
步骤4:发送压力指令 命令:adb shell monkey 1000。 表示monkey随机发送1000个事件

步骤5:获取APP包名 命令:adb “logcat | grep START”。

还有一种获取app的包名和进程名的方式,参考下列连接中的步骤5即可,比较简单。(https://www.jianshu.com/p/5cd011bc1a46)
步骤6:给指定的包打压力 命令:adb shell monkey -p package 1000
例子:如对腾讯扣扣:adb shell monkey -p com.tencent.mobileqq 1000
注意:不要在手机锁屏情况下进行,会报出IO异常

2.Monkey高级参数-throttle实践
throttle:指定事件之间的间隔
adb shell monkey --throttle <milliseconds>

3.Monkey高级参数-seed实践
seed:指定随机生成数的seed值
指定随机值,每次运行都生成进行一样的操作,用于复现某种情况 。
adb shell monkey -s <seed> <event-count>

4.Monkey高级参数-触摸实践实践
触摸事件:设定触摸事件百分比
adb shell monkey --pct-touch <percent>

5.Monkey高级参数-其他事件实践
动作事件:设定动作事件百分比
adb shell monkey --pct-motion <percent>

轨迹球事件:设定轨迹球事件百分比
adb shell monkey --pct-trackball <percent>
基本导航事件:设定基本导航事件百分比,输入设备的上、下、左、右
adb shell monkey --pct-nav <percent>
主要导航事件:设定主要导航事件百分比,兼容中间键、返回键、菜单按键
adb shell monkey --pct-majornav <percent>
系统导航事件:设定系统导航事件百分比,HOME、BACK、拨号及音量键
adb shell monkey --pct-syskeys <percent>
启动Activity事件:设定启动Activity的事件百分比
adb shell monkey --pct-appswitch <percent>
不常用事件:设定不常用事件的百分比
adb shell monkey --pct-anyevent <percent>
崩溃事件:忽略崩溃和异常
adb shell monkey --ignore-crashes <event-count>
超时事件:忽略超时事件
adb shell monkey --ignore-timeouts <event-count>
6.CRASH异常测试结果析取
实践过程:
1.安装可以引起crash的app
adb install app-debug_crash.apk
2.执行压力命令
adb shell monkey -p 包名 1000(执行1000次)
3.析取crash的exception信息



7.ANR异常测试结果析取
实践过程:
1.安装可以引起ANR的app
adb install app_debug_anr.apk
2.执行压力命令
adb shell -p 包名 1000
3.析取ANR的Exception的信息
网友评论