本文共 2293 字,大约阅读时间需要 7 分钟。
在中小团队的业务发展初期,用户量规模有限,测试手动测试的覆盖程度也有限。在业务发展前期而言,将自动化测试和开发分离,是一种有效的提高质量的方法。例如可以每天晚上10点后自动执行压力测试,每次跑10小时,次日早上得到测试报告,如果有问题,自动将邮件发送至相关开发。这样可以及早发现隐蔽的问题,避免用户量多了以后,线上爆更多的问题。
LeakCanary是很多团队都在使用的内存泄漏检测工具,因此最简单的方案就是通过LeakCanary在自动化测试过程中,检测内存泄漏。
但因为LeakCanary在发现泄漏时,会弹窗提示,而monkey测试,可能会存在误操作所以可以在初始化时,屏蔽弹窗。
LeakCanary.install(application);LeakCanaryInternals.setEnabled(application, DisplayLeakActivity.class, false);
简单流程如下:
1.设备开机,解锁
2.启动相应APP
3.切换到全屏模式,避免状态栏和虚拟键干扰
4.启动adb monkey命令
相关脚本如下:
1.开机解锁:
#!/usr/bin/env python #coding:utf8import osimport sysimport subprocess#https://blog.csdn.net/q_539860309/article/details/82856159#get the result printed in cmd linedef RunShellWithReturnCode(command,print_output=True,universal_newlines=True): p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=universal_newlines) if print_output: output_array = [] while True: line = p.stdout.readline() if not line: break print line.strip("/n") output_array.append(line) output ="".join(output_array) else: output = p.stdout.read() p.wait() errout = p.stderr.read() if print_output and errout: print >> sys.stderr, errout p.stdout.close() p.stderr.close() return output, p.returncodestdout, stderr = RunShellWithReturnCode('adb shell dumpsys window policy')#check if the phone screen is on light,if not,open and enter passwordif 'mShowingLockscreen=true' in stdout: if 'mScreenOnEarly=false' in stdout: os.system('adb shell input keyevent 26') os.system('adb shell input swipe 300 300 1000 300')# enter password os.system('adb shell input tap 535 1749') os.system('adb shell input tap 535 1749') os.system('adb shell input tap 535 1749') os.system('adb shell input tap 535 1749')
2.启动APP:
os.system('adb shell am start -n packageName/ActivityName')
3.切换全屏模式:
os.system('adb shell settings put global policy_control immersive.full=*')
4.执行adb命令:(每500ms点击一次,共点击100000次)
os.system('adb shell monkey -p packageName -v 100000 --throttle 500 --pct-syskeys 0')
总结:
本文提供了最简单自动化测试的思路和实现,通过LeakCanary,Monkey来实现压力测试。如果想做的更好,可以将测试过程中出现的ANR和Crash信息保存到本地;另外还可以在本地提供日志文件的分析功能。
最后建议在jenkins中配置单独的monkey工程,这样可以将自动化测试和开发完全分开,提高开发效率。