前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android自动化测试【初级篇】-- Monkey测试

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

作者头像
先知先觉
发布2019-01-21 16:06:44
3K0
发布2019-01-21 16:06:44
举报
文章被收录于专栏:伟大程序猿的诞生

前言

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

由于以下原因:

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

所以呢,出于以上原因我们可以通过自动化测试来帮我们找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脚本

代码语言:javascript
复制
# Script to start "monkey" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/monkey.jar
trap "" HUP
exec app_process $base/bin com.android.commands.monkey.Monkey $*

4.Monkey的使用

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

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

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

代码语言:javascript
复制
adb shell monkey [options]

以下是常用命令:

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

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的基本功能就介绍到这里,下一篇将向大家介绍Monkeyrunner。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年12月15日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • Monkey
    • 1.什么是Monkey
      • 2.Monkey的使用场景
        • 3.Monkey的原理
          • 4.Monkey的使用
            • 5.Monkey选项列表
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档