**前言:**公司盒子将要进行monkey测试, 写此博文做技术储备。
**原文出处:**http://blog.csdn.net/u014158743/article/details/52540705
##monkey测试
####启动你的软件并且触发500个事件
$ adb shell monkey -v -p your.package.name 500
####全部命令选项:
–help 帮助
-v {0,1,2}三级,-v越多,反馈信息更详细
-s 伪随机数生成器
–throttle 延迟执行速度,缺省则尽快
–pct-touch <%> down-up事件百分比
–pct-motion <%> down-[伪随机事件]-up
–pct-trackball <%> 轨迹事件百分比, 轨迹事件为move,有时伴有点击
–pct-nav <%> 基本导航事件百分比,上下左右
–pct-majornav <%> 主要导航事件的百分比,5-way的中间按键,回退,菜单等
–pct-syskeys <%> 系统按键事件的百分比
–pct-appswitch <%> 启动activity的百分比
–pct-anyevent <%> 其它事件的百分比
-p 指定启动包下面的activity
-c 只允许系统指定某个类别列出的activity, 一个c只能用于一个类别
–dbg-no-events 设置此选项, monkey将执行初始启动, 进入到一个测试activity, 然后不再进一步生成事件
–hprof 将在monkey事件序列之前和之后立即生成profiling报告
–ignore-crashes 无视程序崩溃或失控异常
–ignore-timeouts 无视anr异常
–ignore-security-exceptions 无视许可异常
–kill-process-after-error 出错杀死进程
–monitor-native-crashes 监视并报告Android系统中本地代码的崩溃事件
–wait-dbg 停止执行中的monkey, 直到有调试器和它相连接
####执行单一apk:
monkey –p -c -s <限制语句> --throttle -v 执行次数> C:\monkey_test.txt
####执行apk集合:
monkey –pkg-blacklist-file /data/blacklist.txt -c -s <限制语句> --throttle -v 执行次数> C:\blacklist_test.txt //执行黑名单以外的应用
或
monkey –pkg-whitelist-file /data/whitelist.txt -c -s <限制语句> --throttle -v 执行次数 > C:\whitelist_test.txt //执行白名单的应用
####停止monkey
1. monkey运行时:adb shell ps|grep monkey返回的第一个数字就是monkey的进程号
2. adb shell kill [进程号]
##实例小记:
####动作log在命令行:
adb shell monkey -v -p com.xinwo.health --pct-appswitch 0 --throttle 500 100000
####动作log在电脑:
adb shell monkey -v -p com.xinwo.health --pct-appswitch 0 --throttle 500 100000 > ./monkeytest
####动作log在盒子:
adb shell
monkey -v -p com.xinwo.health --pct-appswitch 0 --throttle 500 100000 > /data/monkeytest
根据实际测试需要添加参数
1. --ignore-timeouts
2. --ignore-crashes
3. --ignore-security-exceptions 等.
####查看日志log:
1. as的logcat
2. log在命令行: adb logcat -s tag
3. log在电脑: adb logcat > ./log
4. log在盒子: adb shell 然后 logcat > /data/log
####动作log解析
// 伪随机码和monkey执行次数
:Monkey: seed=1474943047616 count=100
// 被测试应用的包名
:AllowPackage: com.xinwo.health
// 没指定-c默认去找有LAUNCHER或有MONKEY的activity
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:// 执行各动作的百分比
// 0: 15.306122%
// 1: 10.204082%
// 2: 2.0408163%
// 3: 15.306122%
// 4: -0.0%
// 5: 25.510204%
// 6: 15.306122%
// 7: 2.0408163%
// 8: -0.0%
// 9: 1.0204082%
// 10: 13.265306%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.xinwo.health/.activity.MainActivity;end
// 找到了MainActivity并启动
// Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.xinwo.health/.activity.MainActivity } in package com.xinwo.health
// 点击事件
:Sending Touch (ACTION_DOWN): 0:(616.0,101.0)
:Sending Touch (ACTION_UP): 0:(615.6293,106.84831)
// 滑动事件
:Sending Trackball (ACTION_MOVE): 0:(0.0,1.0)
:Sending Touch (ACTION_DOWN): 0:(814.0,380.0)
:Sending Touch (ACTION_UP): 0:(818.69,399.07745)
:Sending Touch (ACTION_DOWN): 0:(301.0,397.0)
:Sending Touch (ACTION_UP): 0:(320.92767,396.68716)
:Sending Touch (ACTION_DOWN): 0:(207.0,405.0)
:Sending Touch (ACTION_UP): 0:(161.20079,390.50723)
:Sending Touch (ACTION_DOWN): 0:(665.0,379.0)
:Sending Touch (ACTION_UP): 0:(674.58777,359.97778)
// Allowing start of Intent { cmp=com.xinwo.health/.activity.TestChooseActivity } in package com.xinwo.health
:Sending Trackball (ACTION_MOVE): 0:(0.0,-5.0)
:Sending Touch (ACTION_DOWN): 0:(282.0,366.0)
:Sending Touch (ACTION_UP): 0:(290.99738,377.07254)
// Allowing start of Intent { cmp=com.xinwo.health/.activity.TestProcessActivity } in package com.xinwo.health
:Sending Touch (ACTION_DOWN): 0:(816.0,416.0)
:Sending Touch (ACTION_UP): 0:(808.4478,401.9289)
:Sending Touch (ACTION_DOWN): 0:(206.0,339.0)
:Sending Touch (ACTION_UP): 0:(287.30112,302.14993)
:Sending Touch (ACTION_DOWN): 0:(303.0,236.0)
// 成功执行了100次
Events injected: 100
:Sending rotation degree=0, persist=false
// 执行时间和在期间内联网等情况.
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=15814ms (0ms mobile, 0ms wifi, 15814ms not connected)
// Monkey finished