class TestRegister: data = excel_handler.read_excel('registe') host = yaml.read_yaml()['host']['url'] @pytest.mark.parametrize("data_info", data) 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
class TestLogin: host = yaml.read_yaml()['host']['url'] data = excel_handler.read_excel('login') @pytest.mark.parametrize("data_info", data) 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
class TestInvest: data = excel_handler.read_excel('invest') host = yaml.read_yaml()['host']['url'] invest_info = yaml.read_yaml()["invest_info"] @pytest.mark.parametrize("data_info", data) 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 replace_data(yaml_key, data_info): """ 替换测试用例中固定的数据 :return: """ info = yaml.read_yaml()[yaml_key] pattern = "#(.*?)#" while re.findall(pattern, data_info): key = re.search(pattern, data_info).group(1) try: data_info = re.sub(pattern, str(info[key]), data_info, 1) except Exception as e: logger.error("替换excel表中固定的{}数据失败".format(key)) raise e return data_info
class TestWithdraw: data = excel_handler.read_excel('withdraw') host = yaml.read_yaml()["host"]["url"] @pytest.mark.parametrize("data_info", data) 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
class TestRecharge: host = yaml.read_yaml()["host"]["url"] data = excel_handler.read_excel("recharge") @pytest.mark.parametrize("data_info", data) 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
""" 增加项目的测试用例 新增一个项目,借款人 id 必须是 member 表中已经存在,新增成功后,会保存一条项目记录到 loan 表中 发送请求获取所有的项目列表,查看是否新增了一个项目 """ import pytest import json from common.baselogger import logger from middleware.yaml_handler import yaml from middleware.excel_handler import excel_handler from middleware.help import replace_data from middleware.help import sub_data from middleware.help import Context host = yaml.read_yaml()["host"]["url"] data = excel_handler.read_excel('addloan') @pytest.mark.add_loan @pytest.mark.usefixtures("init_req") class TestAdd: @pytest.mark.parametrize("data_info", data) 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()
class Context: """ 上下文相关的数据存储 """ host = yaml.read_yaml()["host"]["url"] data = '{"mobilephone":"#mobilephone#","amount":"0"}' def __init__(self, session: BaseRequest): """ :param session: 请求的session对象, """ self.session = session def login(self): """ 登录请求 :return: """ # self.session.run_main() pass def recharge(self): """ 发送充值0元的请求 :return: """ data_info = replace_data("user_info", self.data) res = self.session.run_main('post', self.host + "/member/recharge", data=json.loads(data_info)) return res.json() def get_loan_num(self): res = self.session.run_main(method="get", url=self.host + '/loan/getLoanList') loan_data = res.json()["data"] count = 0 for i in loan_data: count += 1 return count @property def get_loan_id(self): res = self.session.run_main(method="get", url=self.host + '/loan/getLoanList') loan_data = res.json()["data"] for loan in loan_data: if loan["status"] == "1": return loan["id"] @property def memberId(self): """ 获取用户ID :return: """ # 调用充值接口 res = self.recharge() member_id = jsonpath(res, "$..id")[0] return str(member_id) @property def leave_mount(self): res = self.recharge() leave_amount = jsonpath(res, "$..leaveamount")[0] return leave_amount
""" 项目相关的db模块 """ from common.basemysql import BaseMysql from middleware.yaml_handler import yaml class DbHandler(BaseMysql): pass db_info = yaml.read_yaml()['db'] db = DbHandler() # 连接数据库 db_handler = db.connect(host=db_info['host'], port=db_info['port'], user=db_info['user'], password=db_info['password'], database=db_info['database'])