Android自动化测试【初级篇】-- Monkey测试

来源:互联网 发布:access2007数据库教程 编辑:程序博客网 时间:2024/05/18 06:21

前言

为什么需要开发者不适合自测?

由于以下原因:

开发人员对其所写代码有主观认同感
开发人员对软件过于乐观的心态
开发人员对需求易产生偏差与混淆
开发人员擅长修复但不擅长拆解
开发人员缺乏对软件后续开发的展望
开发人员缺乏测试经验和方法

所以呢,出于以上原因我们可以通过自动化测试来帮我们找Bug,不要寄希望于测试。

Monkey

今天这篇文章主要介绍Monkey测试。
monkey官方文档

1.什么是Monkey

Monkey–猴子,顾名思义,就像一只猴子, 在电脑面前,乱敲键盘在测试。
Monkey是一个运行在模拟器或实际设备中的测试工具,他向系统发送伪随机的用户事件流,模拟用户触摸屏幕、滑动Trackball、 按键等操作来对设备上的程序进行压力测试。
主要用于压力测试,看看在这种胡乱点击的状态下,app是否会发生Crash。所有的事件都是随机产生的。

2.Monkey的使用场景

通常在下班前启动Monkey,运行一晚上,第二天早晨看结果。
不过Monkey的优点也是缺点,简单所以不智能,Monky测试使用的事件流数据流是随机的,不能进行自定义,而且测试的对象仅为应用程序包,有一定的局限性。
所以又推出了Monkeyrunner,后面再介绍。

3.Monkey的原理

Monkey程序由Android系统自带,使用Java语言写成,在Android文件系统中的存放路径是: /system/framework/monkey.jar;
Monkey.jar程序是由一个名为“monkey”的Shell脚本来启动执行,shell脚本在Android文件系统中 的存放路径是:/system/bin/monkey;

monkey的shell脚本和monkey.jar本人导出,已上传到CSDN,可自行下载。
下载地址:http://download.csdn.net/download/github_33304260/10160468

如图:Monkey.jar反编译的内容,有兴趣的同学可以自行阅读。

以下是monkey的shell脚本

# Script to start "monkey" on the device, which has a very rudimentary# shell.#base=/systemexport CLASSPATH=$base/framework/monkey.jartrap "" HUPexec app_process $base/bin com.android.commands.monkey.Monkey $*

4.Monkey的使用

Monkey在运行时会生成事件发送给系统,同时对系统进行监测,对以下3种情况进行特殊处理:

1.如果Monkey运行在一个或几个特定的包上,那么当Monkey监测到会跳转到其他包的操作时,会阻止这种操作。
2.如果应用程序崩溃或收到任何未处理的异常,Monkey将停止并报错。
3.如果应用程序产生了ANR,Monkey将停止并报错。

由于Monkey运行在模拟器或者设备中,因此,必须通过shell环境启动。
基本语法:

adb shell monkey [options]

以下是常用命令:

#制定包名为your.package.name的应用程序,并向其发送500个伪随机事件adb shell monkey -p your.package.name -v 500
#可以同时指定多个应用adb shell monkey -p your.package.name1 -p your.package.name2 -v 500
#制定包名为your.package.name的应用程序,并向其发送500个伪随机事件,伪随机数中子值为10#也就是下次再使用伪随机数中子值为10的命令,它将生成相同的事件序列adb shell monkey -p your.package.name -s 10 500
#可以将日志写入到你指定的文件adb shell monkey -p your.package.name -v -s 10 500 > ./your_path/monkey.txt

更多的命令可以参考下面的选项表这里就不一一列出。

5.Monkey选项列表

Monkey有许多选项大致分为4类:

1.基本配置选项:例如设置尝试的事件数量
2.运行约束选项:
3.事件类型和频率
4.调试选项

Category Option DescriptionGeneral--helpPrints a simple usage guide.-vEach -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, andfinal 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-randommovements, 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 asthe 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 maximizingcoverage 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-usedbuttons 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 systemto visit activities within those packages. If your application requires access to activities inother 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 activitiesin 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 categoryIntent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -coption multiple times — one -c option per category.Debugging--dbg-no-eventsWhen specified, the Monkey will perform the initial launch into a test activity, butwill not generate any further events.For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkeyrunning for 30 seconds or more. This provides an environment in which you can monitor packagetransitions invoked by your application.--hprofIf set, this option will generate profiling reports immediately before and afterthe Monkey event sequence.This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more informationon trace files.--ignore-crashesNormally, 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 tothe system, until the count is completed.--ignore-timeoutsNormally, the Monkey will stop when the application experiences any type of timeout error suchas 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-exceptionsNormally, 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 specifythis option, the Monkey will continue to send events to the system, until the count is completed.--kill-process-after-errorNormally, when the Monkey stops due to an error, the application that failed will be leftrunning. When this option is set, it will signal the system to stop the process in which the erroroccurred.Note, under a normal (successful) completion, the launched process(es) are not stopped, andthe device is simply left in the last state after the final event.--monitor-native-crashesWatches for and reports crashes occurring in the Android system native code. If –kill-process-after-error is set, the system will stop.--wait-dbgStops the Monkey from executing until a debugger is attached to it.

Monkey的基本功能就介绍到这里,下一篇将向大家介绍Monkeyrunner。

扫码关注公众号“伟大程序猿的诞生“,更多干货等着你~
扫码关注公众号“伟大程序猿的诞生“,更多干货等着你~
扫码关注公众号“伟大程序猿的诞生“,更多干货等着你~

原创粉丝点击