该代码还存在优化的地方,后续优化方向:结合unittest、ddt、内反射搭建自动化测试框架
实现代码如下:
import os import threading from xctest_app.xc_tools.get_aapt import * from xctest_app.xc_tools.get_devices import * class monkeytest: #调用monkey命令进行测试 def get_test(self,udid,interval,package,count,app_path): #安装软件 os.system(f‘adb -s {udid} install -r {app_path}‘) logfile = ‘../xc_logs/‘ + str(udid).replace(‘:‘, ‘_‘) + ‘.txt‘ cmd1=f‘adb -s {udid} shell monkey --throttle {interval} -v -v -v -p {package} {count} > {logfile}‘ os.system(cmd1) #关闭应用程序 cmd2=f‘adb -s {udid} shell am force-stop {package}‘ os.system(cmd2) self.get_result(udid) #卸载软件 os.system(f‘adb -s {udid} uninstall {package}‘) #日志分析 def get_result(self,udid): logfile = ‘../xc_logs/‘ + str(udid).replace(‘:‘, ‘_‘) + ‘.txt‘ with open(logfile,‘r‘,encoding=‘utf-8‘) as f: con=f.read() if ‘crashed‘ in con or ‘Crash‘ in con: print(f‘设备{udid}:出现了Crash异常‘) elif ‘ANR‘ in con: print(f‘设备{udid}:出现了ANR异常‘) else: print(f‘设备{udid}:未出现异常‘) if __name__ == ‘__main__‘: udid=devices().get_devices() interval=300 app_path=‘../xc_apk/wangyiyunyinyue.apk‘ #调用封装的方法获取app的主包名和activityname package=packagenage_activity().get_packagename(app_path)[0] count=1000 for i in udid: threading.Thread(target=monkeytest().get_test,args=(i,interval,package,count,app_path)).start()
原文:https://www.cnblogs.com/badbadboyyx/p/12132483.html