最近学习了Moneky测试;试着用Python写了执行Monkey测试的脚本,下载地址androidMonkeyTest。Monkey测试的基本概念和用法,这里就不再详细叙述了。网上已经有很多这方面的资料,比如JinZhu的Monkey 测试基本操作介绍。
执行Monkey自动化测试的前提
- PC端下载Android SDK的platform-tools,,并配置系统环境变量
- 手机端打开开发者选项中的ABD调试开关
自动化Monkey测试包括4个部分:
- 获取设备,判断是否连接设备;
- monkey命令的配置;
- 运行monkey测试;
- Monkey测试日志的分析,判断是否存在Crash或ANR等异常。
获取设备
通过执行adb devices
命令获取设备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 执行adb命令 def call_adb(command): command_text = 'adb %s' % command print(command_text) result = subprocess.check_output(command_text).decode('utf') return result # 获取连接设备 def get_devices(): command_result = call_adb("devices") devices = command_result.partition('\n')[2].replace('\n', '').split('\tdevice') return [device for device in devices if len(device) > 2] |
获取Monkey命令参数配置
Monkey命令参数的配置保存在yml
格式的文件中。根据要配置的APP名称,可配置包名、随机种子seed
值和执行次数count
,格式如下:
1 2 3 4 5 |
APP_name: packageName: package_name seed: 2000 count: 10000 |
读取配置文件command_config.yml
中参数,拼接成Monkey命令,比如monkey -p com.*** --throttle 500 -s 2000 --ignore-timeouts --ignore-crashes --monitor-native-crashes -v -v -v 10000 > /sdcard/monkeylog.txt"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
def get_monkey_command(file_name, app): """ get the command for monkey """ monkey_command = 'monkey -p ' file_path = os.path.join(os.getcwd() + '/monkey_config', file_name) file_content = load_yaml_file(file_path) try: command_config = file_content.get(app) monkey_command += command_config.get('packageName', 'com.***') monkey_command = monkey_command + ' --throttle ' + str(command_config.get('throttle', 500)) monkey_command = monkey_command + ' -s ' + str(command_config.get('seed', 2000)) monkey_command = monkey_command + ' --ignore-timeouts --ignore-crashes --monitor-native-crashes -v -v -v ' monkey_command = monkey_command + str(command_config.get('count', 10000)) # the path of log file # monkey_command = monkey_command + ' > ' + '/sdcard/monkeylog.txt' log_file_path = os.path.abspath(os.path.join(os.getcwd(), 'log')) log_file_name = '\monkeyLog_' monkey_command = (monkey_command + ' > ' + log_file_path + log_file_name + datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") + '.txt') return monkey_command except Exception as ex: print('获取monkey参数失败: %s', ex) |
运行Monkey测试
使用adb -s 设备ID shell monkey
启动手机端的monkey
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def start_monkey(file_name, app_name): """ start running monkey """ devices = get_devices() if not devices: raise SystemExit('未获取到设备!') command = get_monkey_command(file_name, app_name) # command = 'adb -s ' + ''.join(devices) + ' shell ' + "\"" + command + "\"" command = 'adb -s ' + ''.join(devices) + ' shell ' + command print(command) subprocess.run(command, shell=True) |
命令行执行Monkey自动化测试脚本,命令是python monkey_test.py command_config.yml appName
。
配置文件command_config.yml
在执行目录下,且已配置appName的monkey参数。
Monkey测试日志的分析,判断是否存在Crash或ANR等异常
读取Monkey测试的日志文件,判断是否存在Crash或ANR等异常。
1 2 3 4 5 6 7 8 9 10 |
file_dir = os.path.join(os.getcwd(), 'log') for log_path, dirs, log_files in os.walk(file_dir): for each_file in log_files: file_path = os.path.join(log_path, each_file) with fileinput.input(file_path) as log: for line in log: if re.findall("ANR", line) or re.findall("CRASH", line) or re.findall("Exception", line): print(log.filename(), log.lineno(), line, end='') |
拜读了,多多学习总是好的!
谢谢你的鼓励!
谢谢美女姐姐!