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 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_register(self, init_req, data_info): # 对excel中的固定的数据进行替换 data_info["data"] = replace_data("user_info", data_info["data"]) # 对excel中的不固定数据进行替换 if "*phone*" in data_info["data"]: phone = get_phone() data_info["data"] = data_info["data"].replace("*phone*", phone) # 发送请求 result = init_req.run_main(method=data_info['method'], url=self.host + data_info["url"], data=json.loads(data_info["data"])) # 获取状态码 code = result.json()["code"] try: # 进行断言 # 1.状态码断言 assert code == str(data_info["expected"]) # 2.查看数据库是否新增对应的新用户 # ... logger.info("PASS") excel_handler.write_excel('registe', data_info["case_id"] + 1, 9, "pass") except AssertionError as e: logger.error("FAIL") excel_handler.write_excel('registe', data_info["case_id"] + 1, 9, "fail") raise e
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