测试基础 monkey 稳定性测试

测试小书童 · 2016年09月12日 · 最后由 测试小书童 回复于 2018年02月05日 · 2312 次阅读

前段时间分享的monkey 压测 app 监控性能

  • monkey 并不合适监控 app,只是用来测试稳定性的,所以只是用来监控报错情况即可,如果需要监控 app 性能,可以集成到 appium 里面,后面有时间再开源

重构后

def get_error(log):
    with open(log, encoding="utf-8") as monkey_log:
        lines = monkey_log.readlines()
        for line in lines:
            if re.findall(go.ANR, line):
                print('\033[1;31;42m')
                print("存在anr错误:", line)
                go.I_ANR += 1
            if re.findall(go.CRASH, line):
                print('\033[1;31;42m')
                print("存在crash错误:", line)
                go.I_CRASH += 1
            if re.findall(go.EXCEPTION, line):
                print('\033[1;31;42m')
                print("存在exception异常:", line)
                go.I_EXCEPTION += 1
        if go.I_ANR == 0 and go.I_CRASH == 0 and go.I_EXCEPTION == 0:
            print("恭喜,没有任何错误")

 # 存手机信息
def get_phome(phonelog):
    bg = BphomeMsg.getPhone("log.txt").get_phone_Kernel()
    logname = phonelog + "_" + bg[0]["phone_model"] + bg[0]["phone_name"] + bg[0]["release"] + ".txt"
    of = OperateFile.base_file(logname, "w+")
    if of.mkdir_file():
        result = "手机型号:" + bg[0]["phone_name"] + "\n"
        result += "手机名字:" + bg[0]["phone_model"] + "\n"
        result += "系统版本:" + bg[0]["release"] + "\n"
        result += "手机分辨率:" + bg[3] + "\n"
        result += "手机运行内存:" + bg[1] + "\n"
        result += "CPU核数:" + bg[2] + "\n"
        of.write_txt(result)

#开始脚本测试
def start_monkey(cmd, logdir, now1):

    # Monkey测试结果日志:monkey_log
    os.popen(cmd )
    print(cmd)

    # Monkey时手机日志,logcat
    logcatname = logdir+"\\"+now1+r"logcat.log"
    cmd2 = "adb logcat -d >%s" %(logcatname)
    os.popen(cmd2)

if __name__ == '__main__':
    ini_file = 'monkey.ini'
    ba = BAdbCommon
    if OperateFile.base_file(ini_file, "r").check_file():
        if ba.attached_devices():
            mconfig = MMonkeyConfig.monkeyconfig()
            mc = BMonkeyConfig.monkeyConfig(mconfig, ini_file)
            # 打开想要的activity
            ba.open_app(mc.package_name, mc.activity)
            temp = ""
             # monkey开始测试
            start_monkey(mc.cmd, mc.logdir, mc.now)
            while True:
                with open(mc.monkey_log, encoding='utf-8') as monkeylog:
                    if monkeylog.read().count('Monkey finished') > 0:
                        print("测试完成咯")
                        get_error(mc.monkey_log)
                        get_phome(mc.phone_msg_log)
                        break
        else:
            print("设备不存在")
    else:
        print(u"配置文件不存在"+ini_file)


  • 读取手机信息
def getModel(self):
    os.system('adb shell cat /system/build.prop >'+self.cmd_log)
    l_list = {}
    with open(self.cmd_log, "r") as f:
        lines = f.readlines()
        for line in lines:
            line = line.split('=')
            #Android 系统,如anroid 4.0
            if (line[0] == 'ro.build.version.release'):
                l_list["release"] = line[1].replace("\n", " ")
                #手机名字
            if (line[0]=='ro.product.model'):
                print(line[1])
                l_list["phone_name"] = line[1].replace("\n", " ")
                #手机品牌
            if (line[0]=='ro.product.brand'):
                 l_list["phone_model"] = line[1].replace("\n", " ")

    # 删除本地存储的手机信息文件
    if os.path.exists(self.cmd_log):
        os.remove(self.cmd_log)
    return l_list

def get_men_total(self):
    os.system("adb shell cat /proc/meminfo >" + self.cmd_log)
    men_total = ""
    with open(self.cmd_log, "r") as f:
            lines = f.readlines()
            for line in lines:
                line = line.split('=')
                if line[0]:
                    men_total = re.findall(r"\d+", line[0])[0]
                    break
    if os.path.exists(self.cmd_log):
        os.remove(self.cmd_log)
    return men_total
# 得到几核cpu
def get_cpu_kel(self):
    os.system("adb shell cat /proc/cpuinfo >" + self.cmd_log)
    cpu_kel = 0
    with open(self.cmd_log, "r") as f:
            lines = f.readlines()
            for line in lines:
                line = line.split(':')
                if line[0].find("processor") >= 0:
                   cpu_kel += 1
    if os.path.exists(self.cmd_log):
        os.remove(self.cmd_log)
    return str(cpu_kel) + "核"
# print(get_cpu_kel("d:\\men.txt"))

# 得到手机分辨率
def get_app_pix(self):
    result = os.popen("adb shell wm size", "r")
    return result.readline().split("Physical size:")[1]

测试结果


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 13 条回复 时间 点赞
if re.findall(go.ANR, line):
    print('\033[1;31;42m')
    print("存在anr错误:", line)
    go.I_ANR += 1

这个 go 模块是你自己写的吗?
之前写的时候就怎么考虑 python 怎么在一边写日志的时候一边读的问题

请问一下 ANR 、CRASH、Excepion,检测日志里的什么字段

#1 楼 @darker50
#2 楼 @newber
ANR 就是定义的相同的字符串变量,I_ANR 就是定义的 int 变量。

我问的不是这个问题,CRASH 是对比日志里那个字段,判断有 crash 异常的

#4 楼 @newber 直接在日志中搜索 ANR 或者 crash 试一下,如果发生异常,会有相关信息的。

#4 楼 @newber if re.findall(go.ANR, line)

代码里有。。。

学习下

好尴尬,实在看不懂,努力 get 中……

ba = BAdbCommon 这个是定义什么呢,有报错 “NameError: name 'BAdbCommon' is not defined”

@newber 一般 anr 的话,直接搜 anr 就行了,或者严格点 anr in,这样过滤。如果是 java crash 的话,用 “fatal” 或者 “fatal exception”, 还有一些是 native crash 的和 reboot 的我也不是很确定。

哈哈,收藏你的工具好久了,一直没有试用,主要是现在工作环境有点坑爹

测试小书童 关闭了讨论 02月05日 21:37
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册