def test_login (test_info): actual_url = test_info['url'] actual_method = test_info['method'] actual_json = test_info['json'] actual_headers = test_info['headers'] expected = test_info['expected'] if '*phone*' in actual_json: mobile_phone =generate_new_phone() actual_json = actual_json.replace('*phone*',mobile_phone) if '#phone#' in actual_json: mobile_phone =user_config['investor_user']['phone'] actual_json = actual_json.replace('#phone#',mobile_phone) if '#pwd#' in actual_json: pwd =user_config['investor_user']['pwd'] actual_json = actual_json.replace('#pwd#',pwd) print(actual_json) res = requests.request(method=actual_method, url=yaml_config['host'] + actual_url, headers=eval(actual_headers), json=eval(actual_json)) res_body = res.json() try: assert res_body['code'] == expected except AssertionError as e: logger.error("用例失败:{}".format(e)) raise e finally: excel = ExcelHandler(excel_file) excel.write('login',str(res_body),row=int(test_info['case_id']+1),column=9) if res_body['code']== expected: excel.write('login',True,row=int(test_info['case_id']+1),column=8) else: excel.write('login',False,row=int(test_info['case_id']+1),column=8)
def test_recharge(info, login): """充值""" """先要替换""" if "#member_id#" in info['json']: info["json"] = info["json"].replace('#member_id#', str(login['id'])) if "#wrong_member_id#" in info['json']: info["json"] = info["json"].replace('#wrong_member_id#', str(login['id'] + 1)) # # token组装方式1:通过excel替换 # if "#token#" in info['headers']: # info["headers"] = info["headers"].replace("#token#",login['token']) # token 组装2:通过headers 添加,excel 表格里面不需要Authorization headers = json.loads(info["headers"]) headers['Authorization'] = login['token'] res = requests.request(url=yaml_config['host'] + info['url'], method=info['method'], headers=headers, json=json.loads(info['json'])) res_body = res.json() print(res_body) try: assert res_body['code'] == info["expected"] except AssertionError as e: logger.error("用例失败:{}".format(e)) raise e finally: excel = ExcelHandler(excel_file) excel.write('recharge', str(res_body), row=int(info['case_id'] + 1), column=9) after_recharge_money = db.query(sql) if json.loads(info["json"])['amount']: money = json.loads(info["json"])['amount'] if res_body['code'] == info["expected"]: excel.write('recharge', True, row=int(info['case_id'] + 1), column=8) if str(money).isdigit(): if after_recharge_money[ 'leave_amount'] == before_recharge_money[ 'leave_amount'] + decimal.Decimal(money): # float(after_recharge_money['leave_amount']) == float(before_recharge_money['leave_amount']) + float(money) excel.write('recharge', "充值成功", row=int(info['case_id'] + 1), column=8) else: excel.write('recharge', False, row=int(info['case_id'] + 1), column=8)
def test_register_01(test_info): # actual_url = 'http://api.lemonban.com/futureloan/member/register' # actual_method = 'POST' # actual_json = {"mobile_phone":"","pwd":""} # actual_headers = {"X-Lemonban-Media-Type":"lemonban.v2"} # expected = 2 # 元组要控制索引 # actual_url = test_info[3] # actual_method = test_info[6] # actual_json = test_info[4] # actual_headers = test_info[5] # expected = test_info[7] # 字典取值 actual_url = test_info['url'] actual_method = test_info['method'] actual_json = test_info['json'] actual_headers = test_info['headers'] expected = test_info['expected'] # 读取 test_info['json'], # 如果存在 # new_phone, if '#new_phone#' in actual_json: # 生成手机号码 13789456789 generate_new_phone mobile_phone = generate_new_phone() # 替换为new_phone actual_json = actual_json.replace('#new_phone#', mobile_phone) # 传入都是字符串 res = requests.request(method=actual_method, url=yaml_config['host'] + actual_url, headers=eval(actual_headers), json=eval(actual_json)) res_body = res.json() print(res_body) try: assert res_body['code'] == expected except AssertionError as e: logger.error("用例失败:{}".format(e)) raise e finally: excel = ExcelHandler(excel_file) excel.write('register', str(res_body), row=int(test_info['case_id']) + 1, column=9) if res_body['code'] == expected: excel.write('register', 'True', row=int(test_info['case_id']) + 1, column=8) else: excel.write('register', 'False', row=int(test_info['case_id']) + 1, column=8)
def test_recharge(info,login): """充值""" """先要替换""" if "#member_id#" in info['json']: info["json"] = info["json"].replace('#member_id#',str(login['id'])) if "#wrong_member_id#" in info['json']: info["json"] = info["json"].replace('#wrong_member_id#', str(login['id'] + 1)) # # token组装方式1:通过excel替换 # if "#token#" in info['headers']: # info["headers"] = info["headers"].replace("#token#",login['token']) # token 组装2:通过headers 添加,excel 表格里面不需要Authorization headers = json.loads(info["headers"]) headers['Authorization'] = login['token'] # 数据库访问,充值之前的余额 db = DBHandler() sql = 'select leave_amount from member where id={}'.format(login['id']) result = db.query(sql) before_recharge_money =result['leave_amount'] db.db_colse() data = json.loads(info['json']) res= requests.request(url= yaml_config['host'] + info['url'], method=info['method'], headers=headers, json= data) res_body = res.json() print(res_body) try: assert res_body['code'] == info["expected"] except AssertionError as e: logger.error("用例失败:{}".format(e)) raise e finally: excel = ExcelHandler(excel_file) excel.write('recharge',str(res_body),row=int(info['case_id']+1),column=9) if res_body['code'] == 0: db = DBHandler() sql = 'select leave_amount from member where id={}'.format(login['id']) result = db.query(sql) after_recharge_money = result['leave_amount'] db.db_colse() money = Decimal(str(data['amount'])) assert before_recharge_money + money == after_recharge_money if res_body['code'] == info["expected"]: excel.write('recharge',True,row=int(info['case_id']+1),column=8) else: excel.write('recharge',False,row=int(info['case_id']+1),column=8)
def test_withdraw(info, login): if "#member_id#" in info['json']: info["json"] = info["json"].replace("#member_id#", str(login['id'])) if "#wrong_member_id#" in info['json']: info["json"] = info["json"].replace('#wrong_member_id#', str(login['id'] + 10)) if "#amount#" in info["json"]: info["json"] = info["json"].replace('#amount#', str(before_recharge_money + 1)) headers = json.loads(info['headers']) headers['Authorization'] = login['token'] res = requests.request(method=info['method'], url=yaml_config['host'] + info['url'], headers=headers, json=json.loads(info['json'])) res_body = res.json() try: assert res_body['code'] == info["expected"] except AssertionError as e: logger.error("用例失败:{}".format(e)) raise e finally: excel = ExcelHandler(excel_file) excel.write('withdraw', str(res_body), row=int(info['case_id'] + 1), column=9) after_recharge_money = db.query(sql)['leave_amount'] if json.loads(info["json"])['amount']: money = json.loads(info["json"])['amount'] if res_body['code'] == info["expected"]: excel.write('withdraw', True, row=int(info['case_id'] + 1), column=8) if str(money).isdigit(): # if after_recharge_money == before_recharge_money - decimal.Decimal(money): if float(after_recharge_money ) == float(before_recharge_money) - float(money): excel.write('withdraw', "充值成功", row=int(info['case_id'] + 1), column=8) else: excel.write('withdraw', False, row=int(info['case_id'] + 1), column=8)
def test_recharge(info, login): """充值""" """先要替换""" if "#member_id#" in info['json']: info["json"] = info["json"].replace('#member_id#', str(login['id'])) if "#wrong_member_id#" in info['json']: info["json"] = info["json"].replace('#wrong_member_id#', str(login['id'] + 1)) # token组装方式1:通过excel替换 if "#token#" in info['headers']: info["headers"] = info["headers"].replace("#token#", login['token']) res = requests.request(url=yaml_config['host'] + info['url'], method=info['method'], headers=json.loads(info['headers']), json=json.loads(info['json'])) res_body = res.json() print(res_body) try: assert res_body['code'] == info["expected"] except AssertionError as e: logger.error("用例失败:{}".format(e)) raise e finally: excel = ExcelHandler(excel_file) excel.write('recharge', str(res_body), row=int(info['case_id'] + 1), column=9) if res_body['code'] == info["expected"]: excel.write('recharge', True, row=int(info['case_id'] + 1), column=8) else: excel.write('recharge', False, row=int(info['case_id'] + 1), column=8)