def run_main(self, method, url, headers=None, data=None, json=None, params=None, **kwargs): """ 调用主函数发送请求 :param method: 请求方法 :param url: 请求地址 :param headers: 请求头信息 :param data: 请求数据,data类型 :param json: 请求数据,json类型 :param params: 请求数据,get :param kwargs: :return: res.json() """ try: reason = self.send(method=method, url=url, headers=headers, data=data, json=json, params=params, **kwargs) except Exception as e: logger.exception("调用主函数失败") raise e return reason
def test_add_loan(self, init_req, data_info): # 对excel中的固定数据进行替换 data_info["data"] = replace_data(yaml_key="add_loan", data_info=data_info["data"]) # 对excel中上下文关联的数据进行替换 data_info["data"] = sub_data(init_req, data_info["data"]) # 发送请求1,获取创建项目前项目的总数 before_loan_num = Context(init_req).get_loan_num() # 发送创建项目请求 res = init_req.run_main(method=data_info["method"], url=host + data_info["url"], data=json.loads(data_info["data"])) # 获取状态码 code = res.json()["code"] if code == '10001': # 发送请求2,获取创建项目成功后项目的总数 after_loan_num = Context(init_req).get_loan_num() expected_loan_num = before_loan_num + 1 # 进行断言 try: # 1.状态码断言 assert code == str(data_info["expected"]) if code == "10001": # 2.请求返回的项目总数断言 assert after_loan_num == expected_loan_num # 3.数据库中的项目总数断言 # .... # 将断言结果写入excel表中 excel_handler.write_excel("addloan", data_info["case_id"] + 1, 9, "pass") logger.info("pass") except AssertionError as e: excel_handler.write_excel("addloan", data_info["case_id"] + 1, 9, "fail") logger.exception("fail") raise e
def test_audit(self, data_info, init_req): # 对excel中的数据进行替换 data_info["data"] = replace_data("audit", data_info["data"]) # 对excel中的带*号数据进行替换 if "*loan_id*" in data_info["data"]: loan_id = Context(init_req).get_loan_id data_info["data"] = data_info["data"].replace("*loan_id*", loan_id) # 发送请求 res = init_req.run_main(method=data_info["method"], url=host + data_info["url"], data=json.loads(data_info["data"])) # 获取状态码 code = res.json()["code"] # 进行断言 try: # 1.状态码断言 assert code == str(data_info["expected"]) # 2.数据库中项目的status是否修改成功 # ... # 将断言结果写入excel中 excel_handler.write_excel("audit", data_info["case_id"] + 1, 9, "pass") logger.info("pass") except AssertionError as e: logger.exception("fail") excel_handler.write_excel("audit", data_info["case_id"] + 1, 9, "fail") raise e
def load_sheet(self, sheet_name): try: self.sheet = self.wb[sheet_name] except Exception as e: logger.exception("加载excel表单失败") raise e logger.info("加载excel表单成功") return True
def write_yaml(self, data): try: with open(self.file_path, mode='w', encoding='utf8') as f: yaml.dump(stream=f, data=data, allow_unicode=True) except Exception as e: logger.exception("写入yaml文件失败") raise e logger.info("写入yaml文件成功 {}".format(data)) return True
def read_yaml(self): try: with open(self.file_path, mode='r', encoding='utf8') as f: res = yaml.load(stream=f, Loader=yaml.FullLoader) except Exception as e: logger.exception("读取yaml文件失败") raise e # logger.info("读取yaml文件成功,数据为:{}".format(res)) return res
def test_withdraw(self, init_req, data_info): # 对excel中固定的数据进行替换 data_info["data"] = replace_data("user_info", data_info["data"]) # 对excel中不固定的数据进行替换(new_phone) if "*mobilephone*" in data_info["data"]: new_phone = get_phone() data_info["data"] = data_info["data"].replace("*mobilephone*", new_phone) data_info["data"] = json.loads(data_info["data"]) # 发送请求1,获取接口返回的初始金额 before_amount = Context(init_req).leave_mount # 发送取现请求 res = init_req.run_main(method=data_info["method"], url=self.host + data_info["url"], data=data_info["data"]) res = res.json() # 获取状态码code code = res["code"] if code == "10001": # 获取接口返回的取现后的金额 after_amount = jsonpath(res, "$..leaveamount")[0] # 获取充值金额 amount = data_info["data"]["amount"] # 预期金额 expected_amount = Decimal(before_amount) - Decimal(amount) # 进行断言 try: # 断言1:状态码断言 assert code == data_info["expected"] # 断言2:接口返回的金额断言 if code == "10001": assert float(after_amount) == float(expected_amount) # 断言3:数据库断言 # ... # 将断言结果写入excel表中 excel_handler.write_excel("withdraw", data_info["case_id"] + 1, 9, "pass") logger.info("pass") except AssertionError as e: logger.exception("fail") # 将断言结果写入excel表中 excel_handler.write_excel("withdraw", data_info["case_id"] + 1, 9, "fail") raise e
def test_recharge(self, data_info, init_req): # 对excel中固定的数据进行替换 data_info["data"] = replace_data("user_info", data_info["data"]) # 对excel中异常的数据进行替换 if "*mobilephone*" in data_info["data"]: phone = get_phone() data_info["data"] = data_info["data"].replace( "*mobilephone*", phone) data = json.loads(data_info["data"]) # 发送请求1获取获取请求前的金额 before_amount = Context(init_req).leave_mount # 发送请求2 res = init_req.run_main(data_info["method"], url=self.host + data_info["url"], data=data) res = res.json() # 获取状态码和充值后返回数据中的金额 code = res["code"] if code == "10001": after_amount = jsonpath(res, "$..leaveamount")[0] expected_amount = Decimal(before_amount) + Decimal(data["amount"]) # 进行断言 try: # 状态码断言 assert code == str(data_info["expected"]) # 返回数据中的金额断言 if code == "10001": assert float(after_amount) == float(expected_amount) # 数据库断言 # .... # 将断言结果写入excel excel_handler.write_excel("recharge", data_info["case_id"] + 1, 9, 'pass') logger.info('pass') except AssertionError as e: logger.exception("fail") excel_handler.write_excel("recharge", data_info["case_id"] + 1, 9, 'fail') raise e
def test_login(self, init_req, data_info): # 对excel中固定的数据进行替换 data_info["data"] = replace_data("user_info", data_info["data"]) # 对excel中的异常数据进行替换 if "*mobilephone*" in data_info["data"]: phone = get_phone() data_info["data"] = data_info["data"].replace("*mobilephone*", phone) # 发送请求 res = init_req.run_main(method=data_info["method"], url=self.host + data_info["url"], data=json.loads(data_info["data"])) # 获取code code = res.json()["code"] # 进行断言 try: # 断言状态码 assert code == str(data_info["expected"]) # 断言结果写入excel excel_handler.write_excel('login', data_info["case_id"] + 1, 9, "PASS") logger.info("PASS") except AssertionError as e: logger.exception("FAIL") excel_handler.write_excel('login', data_info["case_id"] + 1, 9, "FAIL") raise e
def test_invest(self, init_req, data_info): # 对excel中的固定数据进行替换 data_info["data"] = replace_data("user_info", data_info["data"]) # 对excel中的上下关联数据进行替换 data_info["data"] = sub_data(init_req, data_info["data"]) # 将数据由json格式转换为字典形式 data_info["data"] = json.loads(data_info["data"]) # 发送请求1,获取投资前用户的可用余额 before_amount = Context(init_req).leave_mount # 发送投资请求 res = BaseRequest().run_main(method=data_info["method"], url=self.host + data_info["url"], data=data_info["data"]) # 获取code res = res.json() code = res["code"] # 发送请求2,获取投资后用户的可用余额 if code == '10001': after_amount = float(Context(init_req).leave_mount) amount = data_info["data"]["amount"] expected_amount = float(Decimal(before_amount) - Decimal(amount)) # 进行断言 try: # 断言1:状态码code断言 assert code == json.loads(data_info["expected"]) # 断言2:接口返回的金额断言 if code == '10001': assert after_amount == expected_amount # 断言3:数据库中的用户金额断言 # 断言4:invest 表是否新增一条投资记录 # 断言5:是否新增一条流水记录保存到 financeLog 表 # 将断言结果写入excel表中 logger.info("pass") excel_handler.write_excel('invest', data_info["case_id"] + 1, 9, "pass") except AssertionError as e: logger.exception("fail") excel_handler.write_excel('invest', data_info["case_id"] + 1, 9, "fail") raise e
def select(self, sql, args=[], one=True): """ 查询数据库 :param one: :param sql: sql语句 :param args: sql参数 :return: 执行结果 """ if self.conn and self.cur: try: self.cur.execute(sql, args) except Exception as e: logger.exception("执行sql失败 {}{}".format(sql, args)) raise e if one: res = self.cur.fetchone() self.conn.commit() else: res = self.cur.fetchall() self.conn.commit() logger.info("执行sql成功 {}".format(sql, args)) return res else: return "未连接数据库"