class TestWithdraw(unittest.TestCase): excal = ReadExcal(file_path, "withdraw") cases = excal.read_excal() request = SendRequest() db = DB() @data(*cases) def test_withdraw(self, case): #准备用例数据 url = conf.get("env", "url") + case["url"] case["data"] = case["data"].replace("#phone#", conf.get("test_data", "phone")) case["data"] = case["data"].replace("#pwd#", conf.get("test_data", "pwd")) headers = eval(conf.get("env", "headers")) # 判断是否是取现接口,取现接口则加上请求头 if case["interface"].lower() == "withdraw": headers["Authorization"] = self.token_value case["data"] = case["data"].replace("#member_id#", str(self.member_id)) data = eval(case["data"]) expected = eval(case["expected"]) method = case["method"] row = case["case_id"] + 1 #判断是否需要sql校验 if case["check_sql"]: sql = case["check_sql"].format(conf.get("test_data", "phone")) start_money = self.db.find_one(sql)["leave_amount"] response = self.request.send_request(url=url, headers=headers, method=method, json=data) res = response.json() if case["interface"] == "login": #提取用户id保存为类属性 TestWithdraw.member_id = jsonpath.jsonpath(res, "$..id")[0] token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token")[0] TestWithdraw.token_value = token_type + " " + token try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["check_sql"]: sql = case["check_sql"].format(conf.get("test_data", "phone")) end_money = self.db.find_one(sql)["leave_amount"] self.assertEqual(Decimal(str(case["amount"])), start_money - end_money) except AssertionError as e: print(expected) print(res) self.excal.write_excal(row=row, column=8, value="未通过") log.error("用例执行{}未通过".format(case["title"])) log.exception(e) raise e else: self.excal.write_excal(row=row, column=8, value="通过") log.info("用例执行{}通过".format(case["title"]))
class TestUpdate(unittest.TestCase): excel = ReadExcal(file_path, "update") cases = excel.read_excal() request = SendRequest() db = DB() @classmethod def setUpClass(cls) -> None: url = conf.get("env", "url") + "/member/login" headers = eval(conf.get("env", "headers")) data = { "mobile_phone": conf.get("test_data", "phone"), "pwd": conf.get("test_data", "pwd") } response = cls.request.send_request(url=url, headers=headers, method="post", json=data) res = response.json() token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] member_id = jsonpath.jsonpath(res, "$..id")[0] CaseData.member_id = str(member_id) CaseData.token_value = token_type + " " + token @data(*cases) def test_update(self, case): url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) headers["Authorization"] = getattr(CaseData, "token_value") data = eval(replace_data(case["data"])) expected = eval(case["expected"]) row = case["case_id"] + 1 response = self.request.send_request(url=url, method=method, headers=headers, json=data) res = response.json() try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["check_sql"]: sql = replace_data(case["check_sql"]) reg_name = self.db.find_one(sql)["reg_name"] self.assertEqual(reg_name, data["reg_name"]) except AssertionError as e: print(expected) print(res) self.excel.write_excal(row=row, column=8, value="未通过") log.error("测试用例{}未通过".format(case["title"])) log.exception(e) raise e else: self.excel.write_excal(row=row, column=8, value="通过") log.info("测试用例{}通过".format(case["title"]))
class TestRegister(unittest.TestCase): excal = ReadExcal(case_file, "register") cases = excal.read_excal() request = SendRequest() db = DB() @data(*cases) def test_register(self, case): #准备数据 url = conf.get("env", "url") + case["url"] method = case["method"] #生成一个手机号 phone = self.random_phone() #替换用例数据中的手机号 case["data"] = case["data"].replace("#phone#", phone) headers = eval(conf.get("env", "headers")) data = eval(case["data"]) expected = eval(case["expected"]) row = case["case_id"] + 1 response = self.request.send_request(url=url, method=method, headers=headers, json=data) res = response.json() try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["check_sql"]: sql = "SELECT * FROM futureloan.member WHERE mobile_phone={}".format( data["mobile_phone"]) # 查询手机号 count = self.db.find_count(sql) self.assertEqual(1, count) except AssertionError as e: print("预期结果:", expected) print("实际结果:", res) self.excal.write_excal(row=row, column=8, value="未通过") log.error("用例:{},执行未通过".format(case["title"])) log.exception(e) raise e else: self.excal.write_excal(row=row, column=8, value="通过") log.info("用例:{},执行未通过".format(case["title"])) def random_phone(self): phone = "138" n = random.randint(100000000, 999999999) phone += str(n)[1:] return phone
class TestAdd(unittest.TestCase): excel = ReadExcal(file_name, "add") cases = excel.read_excal() request = SendRequest() db = DB() @classmethod def setUpClass(cls): """管理员账户登录""" url = conf.get("env", "url") + "/member/login" data = { "mobile_phone": conf.get("test_data", "admin_phone"), "pwd": conf.get("test_data", "admin_pwd") } headers = eval(conf.get("env", "headers")) response = cls.request.send_request(url=url, method="post", json=data, headers=headers) res = response.json() token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] member_id = jsonpath.jsonpath(res, "$..id")[0] # 将提取的数据保存到CaseData的属性中 CaseData.admin_token_value = token_type + " " + token CaseData.admin_member_id = str(member_id) @data(*cases) def test_add(self, case): url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) headers["Authorization"] = getattr(CaseData, "admin_token_value") data = eval(replace_data(case["data"])) expected = eval(case["expected"]) row = case["case_id"] + 1 response = self.request.send_request(url=url, method=method, json=data, headers=headers) res = response.json() # 第三步:断言(比对预期结果和实际结果) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) # 数据库校验 if case["check_sql"]: loan_id = jsonpath.jsonpath(res, "$..id")[0] CaseData.loan_id = str(loan_id) sql = replace_data(case["check_sql"]) sql_loan_id = self.db.find_one(sql)["id"] self.assertEqual(sql_loan_id, loan_id) except AssertionError as e: print("预期结果:", expected) print("实际结果:", res) self.excel.write_excal(row=row, column=8, value="未通过") log.error("用例:{},执行未通过".format(case["title"])) log.exception(e) raise e else: self.excel.write_excal(row=row, column=8, value="通过") log.info("用例:{},执行未通过".format(case["title"]))
class TestRegister(unittest.TestCase): excal = ReadExcal(file_path, "recharge") cases = excal.read_excal() request = SendRequest() db = DB() @classmethod def setUpClass(cls): #1准备登录数据 url = conf.get("env", "url") + "/member/login" data = { "mobile_phone": conf.get("test_data", "phone"), "pwd": conf.get("test_data", "pwd") } headers = eval(conf.get("env", "headers")) #发送请求,进行登录 response = cls.request.send_request(url=url, method="post", json=data, headers=headers) #获取返回数据 res = response.json() #提取token保存为类属性 token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] #将提取到的token设置成类属性 CaseData.token_value = token_type + " " + token #提取用户id,保存为类属性 CaseData.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) @data(*cases) def test_recharge(self, case): #准备数据 url = conf.get("env", "url") + case["url"] method = case["method"] #替换参数中的用户id case["data"] = replace_data(case["data"]) data = eval(case["data"]) headers = eval(conf.get("env", "headers")) headers["Authorization"] = getattr(CaseData, "token_value") expected = eval(case["expected"]) row = case["case_id"] + 1 #第二步发送请求,获取结果 #发送请求前,获取用户余额 if case["check_sql"]: sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format( conf.get("test_data", "phone")) #查询用户当前余额 start_money = self.db.find_one(sql)["leave_amount"] response = self.request.send_request(url=url, method=method, json=data, headers=headers) res = response.json() #发送完请求后获取余额 if case["check_sql"]: sql = "SELECT leave_amount FROM futureloan.member WHERE mobile_phone={}".format( conf.get("test_data", "phone")) end_money = self.db.find_one(sql)["leave_amount"] #第三步,断言 try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["check_sql"]: self.assertEqual(end_money - start_money, Decimal(str(data["amount"]))) except AssertionError as e: print(expected) print(res) self.excal.write_excal(row=row, column=8, value="未通过") log.error("用例执行{}未通过".format(case["title"])) log.exception(e) raise e else: self.excal.write_excal(row=row, column=8, value="通过") log.info("用例执行{}通过".format(case["title"]))
class TestInvest(unittest.TestCase): excel = ReadExcal(file_path, "invest") cases = excel.read_excal() request = SendRequest() db = DB() @data(*cases) def test_invest(self, case): url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) if case["interface"] != "login": headers["Authorization"] = getattr(CaseData, "token_value") data = eval(replace_data(case["data"])) expected = eval(case["expected"]) row = case["case_id"] + 1 #投资成功后的校验 if case["check_sql"]: # 一,账号金额变化 sql = "SELECT leave_amount FROM futureloan.member WHERE id = {}".format( CaseData.member_id) start_money = self.db.find_one(sql)["leave_amount"] #二,投资记录变化 sql1 = "SELECT * FROM futureloan.invest WHERE member_id = {} and loan_id = {}".format( CaseData.member_id, CaseData.loan_id) start_invest = self.db.find_count(sql1) sql2 = "SELECT * FROM futureloan.financelog WHERE pay_member_id = {}".format( CaseData.member_id) start_financelog = self.db.find_count(sql2) response = self.request.send_request(url=url, method=method, headers=headers, json=data) res = response.json() if case["check_sql"]: sql = "SELECT leave_amount FROM futureloan.member WHERE id = {}".format( CaseData.member_id) end_money = self.db.find_one(sql)["leave_amount"] sql1 = "SELECT * FROM futureloan.invest WHERE member_id = {} and loan_id = {}".format( CaseData.member_id, CaseData.loan_id) end_invest = self.db.find_count(sql1) sql2 = "SELECT * FROM futureloan.financelog WHERE pay_member_id = {}".format( CaseData.member_id) end_financelog = self.db.find_count(sql2) if case["interface"] == "login": token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] CaseData.token_value = token_type + " " + token CaseData.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) if case["interface"] == "add": CaseData.loan_id = str(jsonpath.jsonpath(res, "$..id")[0]) try: self.assertEqual(expected["code"], res["code"]) # self.assertEqual(expected["msg"],res["msg"]) self.assertIn(expected["msg"], res["msg"]) if case["check_sql"]: self.assertEqual(start_money - end_money, Decimal(str(data["amount"]))) self.assertEqual(end_invest - start_invest, 1) self.assertEqual(end_financelog - start_financelog, 1) except AssertionError as e: self.excel.write_excal(row=row, column=8, value="未通过") log.error("测试用例{}未通过".format(case["title"])) log.exception(e) raise e else: self.excel.write_excal(row=row, column=8, value="通过") log.info("测试用例{}未通过".format(case["title"]))