def test_login(info): request_data = info['data'] if '*phone*' in request_data: new_phone = generate_new_phone() request_data = request_data.replace('*phone*', new_phone) if '#phone#' in request_data: phone = user_config['investor_user']['phone'] request_data = request_data.replace('#phone#', phone) if '#pwd#' in request_data: pwd = user_config['investor_user']['pwd'] request_data = request_data.replace('#pwd#', pwd) print(request_data) resp = requests.request(method=info['method'], url=yaml_config['host'] + info['url'], headers=eval(info['headers']), json=eval(request_data)) try: assert resp.json()['msg'] == info['expected'] except AssertionError as e: logger.error(e) logger.info("测试数据:{}".format(request_data)) raise e
def test_login(datas): if '#new_phone#' in datas["data"]: phone = Handler.generate_new_phone() datas["data"] = datas["data"].replace('#new_phone#', phone) if '*phone*' in datas['data']: new_phone = Handler.generate_new_phone() datas['data'] = datas['data'].replace('*phone*', new_phone) datas = json.dumps(datas) # 替换 datas = Handler.replace_data(datas) # 转化成字典 datas = json.loads(datas) resp = requests.request(method=datas['method'], url=Handler.env_config["envurl"] + datas['path'], headers=json.loads(datas['headers']), json=json.loads(datas['data'])) try: assert resp.json()['msg'] == datas['expected'] except AssertionError as e: logger.info("msg不正确,{}测试失败{}".format(datas["title"], e)) raise e # 设置Handler对应的属性。 if datas['extractor']: extrators = json.loads(datas['extractor']) for prop, jsonpath_exp in extrators.items(): # value = 'token' value = jsonpath(resp.json(), jsonpath_exp)[0] # setastr(Handler, "loan_token", "yfowepfpwefwoefowf" setattr(Handler, prop, value)
def send_requests(datas): method = datas["method"] url = Handler.env_config["envurl"] + datas["path"] headers = datas["headers"] # 将字符串转换为字典 data = datas["body"] if '#new_phone#' in data: phone = Handler.generate_new_phone() data = data.replace('#new_phone#', phone) logger.info(data) resp = {} # 接收返回数据 res = "" try: res = requests.request(method, url, headers=eval(headers), json=eval(data), verify=False) resp = res.json() resp["StatusCode"] = str(res.status_code) # 状态码转成str resp["time"] = str(res.elapsed.total_seconds()) # 接口请求时间转str if resp["StatusCode"] != "200": resp["error"] = resp["text"] resp["return_code"] = str(res.status_code) else: resp["error"] = "" except Exception as msg: resp["msg"] = str(msg) resp["text"] = res.content.decode("utf-8") # 打印错误返回信息以文本形式输出 resp["return_code"] = "error" resp["rowNum"] = datas["rowNum"] logger.info("resp{}".format(resp)) return resp
def test_withdraw(datas): datas = json.dumps(datas) # 替换 datas = Handler.replace_data(datas) # 转化成字典 datas = json.loads(datas) resp = requests.request(method=datas['method'], url=Handler.env_config["envurl"] + datas['path'], headers=json.loads(datas['headers']), json=json.loads(datas['data'])) # 断言code是否正确 try: assert resp.json()['code'] == datas['expected'] except AssertionError as e: logger.info("code不正确,{}测试失败{}".format(datas["title"], e)) raise e # 设置Handler对应的属性。 if datas['extractor']: extrators = json.loads(datas['extractor']) for prop, jsonpath_exp in extrators.items(): value = jsonpath(resp.json(), jsonpath_exp)[0] setattr(Handler, prop, value)
def test_register(self, test_data): # 打印一个开始的logger信息 logger.info("测试用例:{}执行中。。。".format(test_data['case_title'])) # 访问接口,得到实际结果 res = self.request.visit(method=test_data['method'], url=config.host + test_data['url'], json=json.loads(test_data['data']), headers=json.loads(test_data['headers'])) # 进行断言 try: self.assertEqual( json.loads(test_data['excepted_result'])['code'], res['code']) # 打印日志信息 logger.info("测试用例:{}-OK".format(test_data["case_title"])) # 测试结果赋值Pass self.result = "Pass" except AssertionError as e: # 打印日志信息 logger.info("测试用例:{}-Error,报错:{}".format(test_data['case_title'], e)) # 测试结果赋值Fail self.result = "Fail" # 抛出异常 raise e finally: # 将测试的实际结果写到excel表中 excel_handler.change_cell(config.data_path, "case_login", int(test_data['case_id']) + 1, 9, str(res)) # 将测试结果写入excel表中 excel_handler.change_cell(config.data_path, "case_login", int(test_data['case_id']) + 1, 10, self.result)
def test_withdraw(datas): datas = json.dumps(datas) # 替换 datas = Handler.replace_data(datas) # 转化成字典 datas = json.loads(datas) if json.loads(datas['expected'])['code'] == 1002: datas['data'] = datas['data'].replace( str(json.loads(datas['data'])['amount']), str(json.loads(datas['data'])['amount'] + 1)) money_before = Decimal(0) if json.loads(datas['expected'])['code'] in ( 0, 1002) and 'member_id' in datas['data']: sql = 'select leave_amount from member where id={}'.format( json.loads(datas['data'])['member_id']) result = Handler.db_class.query(sql, one=True) money_before = Decimal(result['leave_amount']) logger.info("充值前账号金额:{}".format(money_before)) resp = requests.request(method=datas['method'], url=Handler.env_config["envurl"] + datas['path'], headers=json.loads(datas['headers']), json=json.loads(datas['data'])) # 断言code是否正确 try: assert resp.json()['code'] == json.loads(datas['expected'])['code'] except AssertionError as e: logger.info("code不正确,{}测试失败{}".format(datas["title"], e)) raise e # 接口返回值断言提现前后账户金额是否正确 # try: # if resp.json()['code'] == 0 and 'member_id' in datas['data']: # assert json.loads(datas['data'])['amount'] - json.loads(datas['data'])['amount'] == resp.json()['data']['leave_amount'] # 账户提现前金额 - 提现金额= 账户提现后金额 # logger.info("提现前金额{} - 提现金额{} == 提现后金额{}".format(datas['extractor']['money'],datas['data']['amount'],resp.json()['data']['leave_amount'])) # except AssertionError as err: # logger.info("提现前后账户金额不正确,{}测试失败{}".format(datas["title"],err)) # raise err # 数据库查询断言提现前后账户金额是否正确 if resp.json()['code'] == 0 and 'member_id' in datas['data']: sql = 'select leave_amount from member where id={}'.format( json.loads(datas['data'])['member_id']) result = Handler.db_class.query(sql, one=True) money_after = result['leave_amount'] money = Decimal(json.loads(datas['data'])['amount']) assert money_before - money == money_after logger.info("提现前账号金额{},提现金额{},提现后账户总金额{}".format( money_before, money, money_after)) # 设置Handler对应的属性。 if datas['extractor']: extrators = json.loads(datas['extractor']) for prop, jsonpath_exp in extrators.items(): value = jsonpath(resp.json(), jsonpath_exp)[0] setattr(Handler, prop, value)
def test_audit(datas, admin_login, add_loan): """审核接口""" datas = json.dumps(datas) # 替换 datas = Handler.replace_data(datas) # 转化成字典 datas = json.loads(datas) resp = requests.request(method=datas['method'], url=Handler.env_config["envurl"] + datas['path'], headers=json.loads(datas['headers']), json=json.loads(datas['data'])) logger.info("resp:{}".format(resp.json())) expected = json.loads(datas['expected']) # 第一版多值断言 # assert resp.json()['code'] == expected["code"] # assert resp.json()['msg'] == expected['msg'] # 第二版多值断言 # for key, value in expected.items(): # # ("code", 0) # # ("msg", "OK") # # try: # assert resp.json()[key] == value # 第三版多值断言 for key, value in expected.items(): # 实际结果的value 怎么获取 assert jsonpath(resp.json(), key)[0] == value # 设置Handler对应的属性。 if datas['extractor']: extrators = json.loads(datas['extractor']) for prop, jsonpath_exp in extrators.items(): value = jsonpath(resp.json(), jsonpath_exp)[0] setattr(Handler, prop, value)
def test_recharge(datas): datas = json.dumps(datas) # 替换 datas = Handler.replace_data(datas) # 转化成字典 datas = json.loads(datas) money_before = Decimal(0) if datas['expected'] == 0 and 'member_id' in datas['data']: sql = 'select leave_amount from member where id={}'.format( json.loads(datas['data'])['member_id']) result = Handler.db_class.query(sql, one=True) money_before = Decimal(result['leave_amount']) logger.info("充值前账号金额:{}".format(money_before)) resp = requests.request(method=datas['method'], url=Handler.env_config["envurl"] + datas['path'], headers=json.loads(datas['headers']), json=json.loads(datas['data'])) try: assert resp.json()['code'] == datas['expected'] except AssertionError as e: logger.info("code不正确,{}测试失败{}".format(datas["title"], e)) raise e if resp.json()['code'] == 0 and 'member_id' in datas['data']: sql = 'select leave_amount from member where id={}'.format( json.loads(datas['data'])['member_id']) result = Handler.db_class.query(sql, one=True) money_after = result['leave_amount'] money = Decimal(json.loads(datas['data'])['amount']) assert money_before + money == money_after logger.info("充值前账号金额{},充值金额{},充值后账户总金额{}".format( money_before, money, money_after)) # 设置Handler对应的属性。 if datas['extractor']: extrators = json.loads(datas['extractor']) for prop, jsonpath_exp in extrators.items(): # value = 'token' value = jsonpath(resp.json(), jsonpath_exp)[0] # setastr(Handler, "loan_token", "yfowepfpwefwoefowf" setattr(Handler, prop, value)