def test_case_oauth_unbind(self, case): data_on = { 'user_id': '*userid*', 'token': '*token*', 'type': '2', 'openid': '2', 'nick_name': '测试', 'head_img': 'ganm.png' } url = apiURL + case.url method = case.method excepted = eval(case.excepted) row = case.case_id + 1 title = case.title data_1 = eval(myconf.get('data', 'data_login')) data_modify_pwd = Md5().sign(data_1) res = self.http.requests(method=method, url=myconf.get('url', 'url3'), data=data_modify_pwd) data_ = res.json().get('data') try: data_.get('token') and data_.get('user_id') except AttributeError as e: print('\033[31m访问登录接口失败,未能获取到userid和token!!') log.debug('访问获取userid和token的登录接口失败,未能获取到userid和token') raise e else: # 获取userid user_token = data_.get('token') # 获取token user_id = data_.get('user_id') user1 = eval(case.data).get('user_id') if '*' in user1: case.data = case.data.replace(user1, user_id)
def test_case_audit(self, case): """审核接口用例执行的逻辑""" #准备测试用例 url = myconf.get('url', 'url') + case.url # 拼接完整的URL地址 row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) # 判断是否有*memberId*的参数需要替换 if "*loan_id*" in case.data: max_id = self.db.find_one("select max(id) from loan")[ 0] # findone()返回的是元组,要加下标才能获取到元素 loan_id = max_id + 1 # 在数据库中查找到最大的id号,加1后替换给*memberId* case.data = case.data.replace("*loan_id*", str(loan_id)) #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response = self.http.request(method=case.method, url=url, data=eval(case.data)) res = response.json() print(res) #判断是否是执行的加标用例 if case.interface == "加标": loan_id = self.db.find_one( "select id from loan where memberId ='{}' order by id desc". format(myconf.get("data", "memberId")))[0] #"SELECT Id FROM loan WHERE MemberId='{}' ORDER BY id DESC".format(myconf.get('data', 'memberId'))) # 将添加的标id,保存为临时变量 setattr(ConText, 'loan_id', loan_id) #对象(类)属性名 属性值
def test_case_relation_list(self, case): url = apiURL + case.url method = case.method excepted = eval(case.excepted).get('msg') row = case.case_id + 1 title = case.title data_1 = eval(myconf.get('data', 'data_login')) data_modify_pwd = Md5().sign(data_1) res = self.http.requests(method=method, url=myconf.get('url', 'url3'), data=data_modify_pwd) data_ = res.json().get('data') try: data_.get('token') and data_.get('user_id') except AttributeError as e: print('\033[31m访问登录接口失败,未能获取到userid和token!!') log.debug('访问获取userid和token的登录接口失败,未能获取到userid和token') raise e else: # 获取userid user_token = data_.get('token') # 获取token user_id = data_.get('user_id') user1 = eval(case.data).get('user_id') if '*' in user1: case.data = case.data.replace(user1, user_id)
def __init__(self): # 建立连接 self.coon = pymysql.connect( host=myconf.get('mysql', 'host'), # 数据库地址 port=myconf.getint('mysql', 'port'), # 端口 user=myconf.get('mysql', 'user'), # 用户名 password=myconf.get('mysql', 'password'), # 密码 database=myconf.get('mysql', 'database'), # 数据库名 charset="utf8" # 指定编码格式 ) # 创建一个游标 self.cur = self.coon.cursor()
def __init__(self): # 第一步,连接到数据库,创建游标 self.conn = pymysql.connect( host=myconf.get("mysql", "host"), # 数据库地址 port=myconf.getint("mysql", "port"), # 端口 user=myconf.get("mysql", "user"), password=myconf.get("mysql", "password"), database=myconf.get("mysql", "database"), # 数据库名 charset="utf8" #指定编码格式 ) # 创建游标 self.cur = self.conn.cursor()
def tearDown(self): data_on = {'user_id': '*userid*', 'token': '*token*', 'relation_user_id': '21625'} data_1 = eval(myconf.get('data', 'data_login')) data_modify_pwd = Md5().sign(data_1) res = self.http.requests(method='post', url=apiURL + '/users/login', data=data_modify_pwd) data_ = res.json().get('data') user_token = data_.get('token') user_id = data_.get('user_id') data_on['user_id'] = user_id data_on['token'] = user_token url2 = myconf.get('url', 'url_t2') + '/users/user_relation' res_on = self.http.requests(method='post', url=url2, data=Md5().sign(data_on)) print(res_on.json()) print('------------最后执行-------')
def test_case_del_user_relation(self, case): data_list = {'user_id': '*userid*', 'token': '*token*'} url = apiURL + case.url method = case.method excepted = eval(case.excepted) row = case.case_id + 1 title = case.title data_1 = eval(myconf.get('data', 'data_login')) data_modify_pwd = Md5().sign(data_1) res = self.http.requests(method=method, url=apiURL + '/users/login', data=data_modify_pwd) data_ = res.json().get('data') try: data_.get('token') and data_.get('user_id') except AttributeError as e: print('\033[31m访问登录接口失败,未能获取到userid和token!!') log.debug('访问获取userid和token的登录接口失败,未能获取到userid和token') raise e else: # 获取userid user_token = data_.get('token') # 获取token user_id = data_.get('user_id') # 获取关联列表,查询relationid,将测试数据中的relationid替换为查询到的id data_list['user_id'] = user_id data_list['token'] = user_token url2 = apiURL + '/users/relation_list' res_l = self.http.requests(method='post', url=url2, data=Md5().sign(data_list)) res_list = res_l.json().get('data') rel_id = res_list[0].get('relation_id') print('获取到的relationid为', rel_id) rela_id = eval(case.data).get('relation_id') if '*' in rela_id: case.data = case.data.replace(rela_id, str(rel_id))
def test_case_login(self,case): """登录接口用例执行的逻辑""" #准备测试用例 url=myconf.get('url',"url")+case.url#字符串连接用+ #url=case.url #data=eval(case.data) method=case.method excepted=eval(case.excepted) row=case.case_id + 1 # 替换用例参数 # if "#phone#" in case.data: #手机号和密码都要替换,这样比较繁琐,封装正则类来替换 # case.data.replace("#phone#",myconf.get("data","phone")) data=replace(case.data) #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response=self.http.request(method=method,url=url,data=eval(data)) res=response.json() #对比预期结果 #self.assertEqual(excepted,res) try: self.assertEqual(excepted,res) except AssertionError as e: """测试用例未通过""" self.excel.write_data(row, 8, '未通过') log.debug("{},该条用例执行未通过".format(case.title)) log.error(e) raise e else: self.excel.write_data(row, 8, '通过') log.debug("{},该条用例执行通过".format(case.title))
def test_case_recharge(self, case): """充值接口用例执行的逻辑""" #准备测试用例数据 url = myconf.get('url', "url") + case.url # 拼接完整的URL地址 row = case.case_id + 1 #替换用例参数 case.data = replace(case.data) if case.check_sql: case.check_sql = replace(case.check_sql) start_money = ReadSQL().find_one(case.check_sql)[0] print('充值之前用户的余额为{}'.format(start_money))
def test_case_del_search_log(self, case): url = apiURL + case.url method = case.method excepted = eval(case.excepted) row = case.case_id + 1 title = case.title data_1 = eval(myconf.get('data', 'data_login')) data_modify_pwd = Md5().sign(data_1) res = self.http.requests(method=method, url=apiURL + '/users/login', data=data_modify_pwd) data_ = res.json().get('data') try: data_.get('token') and data_.get('user_id') except AttributeError as e: print('\033[31m访问登录接口失败,未能获取到userid和token!!') log.debug('访问获取userid和token的登录接口失败,未能获取到userid和token') raise e else: # 获取userid user_token = data_.get('token') # 获取token user_id = data_.get('user_id') data_s = eval(myconf.get('data', 'data_com')) data_user = self.com.login() data_s.update(data_user) data_modify_pwd = Md5().sign(data_s) res = self.http.requests(method=method, url=apiURL + '/users/search_history', data=data_modify_pwd) data_ = res.json().get('data') searchid = [] for i in data_: searchid.append(i.get('id')) print(searchid) user1 = eval(case.data).get('user_id') if '*' in user1: case.data = case.data.replace(user1, user_id)
def test_case_withdraw(self, case): """取现接口用例执行的逻辑""" #准备测试用例数据 url = myconf.get('url', "url") + case.url # 拼接完整的URL地址 data = eval(case.data) method = case.method row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) if case.check_sql: case.check_sql = replace(case.check_sql) start_money = ReadSQL().find_one(case.check_sql)[0] print('取现之前用户的余额为{}'.format(start_money))
def test_case_goods_collect_cancel(self, case): url = apiURL + case.url method = case.method excepted = eval(case.excepted) row = case.case_id + 1 title = case.title data_1 = eval(myconf.get('data', 'data_login')) data_modify_pwd = Md5().sign(data_1) res = self.http.requests(method=method, url=apiURL + '/users/login', data=data_modify_pwd) data_ = res.json().get('data') data_coll = { 'user_id': '*userid*', 'token': '*token*', 'page': '1', 'pageSize': '10' } userdata = self.com.login() data_coll.update(userdata) data_modify_pwd = Md5().sign(data_coll) res = self.http.requests(method='post', url=apiURL + '/users/goods_collect_list', data=data_modify_pwd) # print('________', res.json()) collect_data = res.json().get('data') collect_list = collect_data.get('collect_list') goodsid = [] for i in collect_list: goodsid.append(i.get('goods_id')) # print('00000000', goodsid) try: data_.get('token') and data_.get('user_id') except AttributeError as e: print('\033[31m访问登录接口失败,未能获取到userid和token!!') log.debug('访问获取userid和token的登录接口失败,未能获取到userid和token') raise e else: # 获取userid user_token = data_.get('token') # 获取token user_id = data_.get('user_id') user1 = eval(case.data).get('user_id') if '*' in user1: case.data = case.data.replace(user1, user_id)
def replace(data): while re.search(r"#(.+?)#", data): res = re.search(r"#(.+?)#", data) #提取要替换的内容 r_data = res.group() #print(r_data) #获取要替换的字段 key = res.group(1) #print(key) # 去配置文件中读取字段对应的数据 #捕获异常,如果在配置文件中没有找到固定的值,就去临时变量里找 try: value = myconf.get("data", key) except: value = getattr(ConText, key) #进行替换 data = re.sub(r_data, str(value), data) #re.sub(正则式,替换的字符串,被替换的原始字符串) return data
def test_case_is_exempt_pay(self, case): url = apiURL + case.url method = case.method excepted = eval(case.excepted).get('msg') row = case.case_id + 1 title = case.title status = case.status data_1 = eval(myconf.get('data', 'data_login')) if status == 'no': username = self.data_register.get('username') password = self.data_register.get('password') data_3 = {'username': username, 'password': password} print(data_3) get_data = self.data_res.get_userid_token(data_3) case.data = case.data.replace(eval(case.data).get('user_id'), get_data.get('user_id')) case.data = case.data.replace(eval(case.data).get('token'), get_data.get('token')) else: get_data = self.data_res.get_userid_token(data_1) case.data = case.data.replace(eval(case.data).get('user_id'), get_data.get('user_id')) case.data = case.data.replace(eval(case.data).get('token'), get_data.get('token')) data_2 = eval(case.data) data = Md5().sign(data_2) print('传入的数据为:', data) log.info('正在请求地址{}'.format(url)) response = self.http.requests(method=method, url=url, data=data) print('返回的数据是:', response.json()) res = response.json().get('msg') print('接口请求返回的结果为:', res, type(res)) print('--------期望结果为:', excepted, type(excepted)) try: self.assertEqual(excepted, res) except AssertionError as e: self.excel.write_data(row=row, column=9, value='未通过') log.debug('该条用例未通过{}'.format(title)) raise e else: self.excel.write_data(row=row, column=9, value='通过') log.debug('该条用例通过{}'.format(title))
def test_case_add(self, case): """加标接口用例执行的逻辑""" #准备测试用例 url = myconf.get('url', 'url') + case.url # 拼接完整的URL地址 row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) #判断是否有*memberId*的参数需要替换 if "*memberId*" in case.data: max_id = self.db.find_one("select max(id) from member")[ 0] #findone()返回的是元组,要加下标才能获取到元素 memberid = max_id + 1 #在数据库中查找到最大的id号,加1后替换给*memberId* case.data = case.data.replace( "*memberId*", str(memberid)) #字符串替换的方法,要记得转换类型为str,否则会报错 #判断是否需要SQL校验 if case.check_sql: #如果case文件里的check_sql有数据,就做替换。 case.check_sql = replace( case.check_sql) #调用封装好的替换类,把其中含有“#memberId#"的替换为从配置文件中获取的值 # 获取当前用户加标前的标数量 start_count = self.db.find_count(case.check_sql)
def test_case_register(self, case): """登注册接口用例执行的逻辑""" #准备测试用例 url = myconf.get('url', 'url') + case.url # 拼接完整的URL地址 method = case.method excepted = eval(case.excepted) row = case.case_id + 1 # 替换用例参数 case.data = replace(case.data) #随机生成手机号码 phone = self.random_phone() print(phone) #替换动态化的参数,字符串替换方法 case.data = case.data.replace("*phone*", phone) #加星号是为了避免替换掉其他的同名字符,如mobilephone #发送请求到接口,获取结果 log.info("正在请求地址{}".format(url)) response = self.http.request(method=method, url=url, data=eval(case.data)) res = response.json() #对比预期结果 try: self.assertEqual(excepted, res) # 判断是否需要进行SQL校验 if case.check_sql: # 如果此字段有数据,条件成立 # 用随机生成的手机号替换需要校验的SQL语句中的手机号 case.check_sql = case.check_sql.replace('*phone*', phone) db_res = self.db.find_count(case.check_sql) self.assertEqual(1, db_res) except AssertionError as e: """测试用例未通过""" self.excel.write_data(row, 8, '未通过') log.info("{},该条用例执行未通过".format(case.title)) log.exception(e) raise e else: self.excel.write_data(row, 8, '通过') log.info("{},该条用例执行通过".format(case.title))
# 获取token user_id = data_.get('user_id') user1 = eval(case.data).get('user_id') if '*' in user1: case.data = case.data.replace(user1, user_id) token1 = eval(case.data).get('token') if '*' in token1: case.data = case.data.replace(token1, user_token) data_on['user_id'] = user_id data_on['token'] = user_token ty = eval(case.data).get('type') data_on['type'] = ty url2 = myconf.get('url', 'url_t2') + '/users/oauth_bind' res_on = self.http.requests(method=method, url=url2, data=Md5().sign(data_on)) print(res_on.json()) data_2 = eval(case.data) data = Md5().sign(data_2) print('传入的数据为:', data) log.info('正在请求地址{}'.format(url)) response = self.http.requests(method=method, url=url, data=data) res = response.json() print('接口请求返回的结果为:', res, type(res)) print('--------期望结果为:', excepted, type(excepted)) try:
# -*- coding: utf-8 -*- # @Time : 2019/8/27 20:32 # @File : 日志封装.py # @Software: PyCharm #@author:liu import logging from common.config import myconf import os from common.constant import LOG_DIR log_level = myconf.get("log", "log_level") sh_level = myconf.get("log", "s_level") fh_level = myconf.get("log", "f_level") name = myconf.get("log", "filename") #拼接日志文件路径 file_path = os.path.join(LOG_DIR, name) class Mylogging(object): def __new__(cls, *args, **kwargs): #第一步:创建一个日志收集器,设置收集的等级 mylog = logging.getLogger("my_log") mylog.setLevel(log_level) #第二步:创建日志输出渠道,设置输出等级 sh = logging.StreamHandler() sh.setLevel(sh_level) fh = logging.FileHandler(file_path, encoding="utf8") fh.setLevel(fh_level) #第三步:将日志收集器和输出渠道进行绑定