class TestLogin(unittest.TestCase): '''登录接口测试''' Worksheet_name = "login" excel, data_list = com_excel_read(Worksheet_name) @classmethod def setUpClass(cls): cls.excel.wb = openpyxl.load_workbook(cls.excel.path) def setUp(self): pass @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: respone = com_request(TestLogin, self.Worksheet_name, **kwargs) com_assertEqual(self, respone, eval(kwargs["expect"])) except AssertionError as e: log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()
class TestInvest(unittest.TestCase): '''投资项目''' Worksheet_name = "invest" excel, data_list = com_excel_read(Worksheet_name) @classmethod def setUpClass(cls): '''登录普通用户获取token,用户id''' cls.excel.wb = openpyxl.load_workbook(cls.excel.path) # 重新载入workbook params = config.get("PRESET", "loan_params") respone = com_request(TestInvest, "login", params=params) cls.token = "Bearer" + " " + jsonpath(respone, "$..token")[0] cls.member_id = jsonpath(respone, "$..id")[0] cls.audit_loan = eval(config.get("SETUPDATA", "audit_loan")) cls.loan_id, cls.amount = cls.audit_loan["loan_id"], cls.audit_loan["amount"] def setUp(self): pass @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: if kwargs["sql_cheack"]: # 存在sql校验,获取投资的金额 sql = "select leave_amount from futureloan.member where id=%s" leaveamount_b = mysql.sql_read(sql, self.member_id)["leave_amount"] # 保留投资前的余额 sql = "select count(*) from futureloan.financelog where pay_member_id=%s" count_b = mysql.sql_read(sql, self.member_id)["count(*)"] # 保留投资前的条目数 respone = com_request(TestInvest, self.Worksheet_name, **kwargs, token=self.token) if respone["code"] == 0: # 当投资成功时减去已投资,不论预期是否成功 TestInvest.amount -= int(self.params["amount"]) com_assertEqual(self, respone, eval(kwargs["expect"])) if kwargs["sql_cheack"]: # 存在sql校验,获取投资的金额 sql = "select amount from futureloan.invest where id=%s" res = mysql.sql_read(sql, jsonpath(respone, "$..id")) self.assertEqual(decimal.Decimal(res["amount"]), decimal.Decimal(self.params["amount"])) # 校验投资金额与预期 sql = "select leave_amount from futureloan.member where id=%s" leaveamount_e = mysql.sql_read(sql, self.member_id)["leave_amount"] # 保留投资后的余额 self.assertEqual(leaveamount_b-leaveamount_e, decimal.Decimal(self.params["amount"])) # 校验投资后条目数与预期 sql = "select count(*) from futureloan.financelog where pay_member_id=%s" count_e = mysql.sql_read(sql, self.member_id)["count(*)"] # 保留投资后的条目数 self.assertEqual(count_e-count_b, 1) # 校验条目数 except AssertionError as e: log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()
class TestWithdraw(unittest.TestCase): '''提现接口测试''' Worksheet_name = "withdraw" excel, data_list = com_excel_read(Worksheet_name) leave_amount_max = config.get("PRESET", "leave_amount_max") # 获取提取上限 @classmethod def setUpClass(cls): '''登录获取token,用户id''' cls.excel.wb = openpyxl.load_workbook(cls.excel.path) # 重新载入workbook params = config.get("PRESET", "loan_params") respone = com_request(TestWithdraw, "login", params=params) cls.token = "Bearer" + " " + jsonpath(respone, "$..token")[0] cls.member_id = jsonpath(respone, "$..id")[0] def setUp(self): pass @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: if kwargs["sql_cheack"]: # 存在sql校验,获取初期余额 res = mysql.sql_read(kwargs["sql_cheack"], self.member_id) amount_prime = res["leave_amount"] respone = com_request(TestWithdraw, self.Worksheet_name, **kwargs, token=self.token) if kwargs["sql_cheack"]: # 存在sql校验,获取提现后余额 res = mysql.sql_read(kwargs["sql_cheack"], self.member_id) amount_new = res["leave_amount"] amount = amount_prime - amount_new print(f"实际{info[:2]}:", float(amount)) print(f"预期{info[:2]}:", float(self.params["amount"])) self.assertEqual(float(amount), float(self.params["amount"])) # 校验提现的余额实际与预期 com_assertEqual(self, respone, eval(kwargs["expect"])) except AssertionError as e: print(e) log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()
class TestAdd(unittest.TestCase): '''新增项目''' Worksheet_name = "add" excel, data_list = com_excel_read(Worksheet_name) @classmethod def setUpClass(cls): '''登录获取token,用户id''' cls.excel.wb = openpyxl.load_workbook(cls.excel.path) # 重新载入workbook params = config.get("PRESET", "loan_params") respone = com_request(TestAdd, "login", params=params) cls.token = "Bearer" + " " + jsonpath(respone, "$..token")[0] cls.member_id = jsonpath(respone, "$..id")[0] def setUp(self): pass @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: respone = com_request(TestAdd, self.Worksheet_name, **kwargs, token=self.token) com_assertEqual(self, respone, eval(kwargs["expect"])) if kwargs["sql_cheack"]: # 存在sql校验,获取新增项目名称 res = mysql.sql_read(kwargs["sql_cheack"], jsonpath(respone, "$..id")) print("实际项目名称:", res["title"]) print("预期项目名称:", self.params["title"]) self.assertEqual(res["title"], self.params["title"]) # 校验更新后的昵称与预期 except AssertionError as e: log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()
class TestRegister(unittest.TestCase): '''注册接口测试''' Worksheet_name = "register" excel, data_list = com_excel_read(Worksheet_name) @classmethod def setUpClass(cls): cls.excel.wb = openpyxl.load_workbook(cls.excel.path) def setUp(self): pass @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: respone = com_request(TestRegister, self.Worksheet_name, **kwargs) com_assertEqual(self, respone, eval(kwargs["expect"])) if kwargs["sql_cheack"]: # 存在sql校验,获取新增注册名称 res = mysql.sql_read( kwargs["sql_cheack"], jsonpath(respone.json(), "$..username")[0]) print("实际项目名称:", res["username"]) print("预期项目名称:", self.params["username"]) self.assertEqual(res["username"], self.params["username"]) # 校验更新后的昵称与预期 except AssertionError as e: log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()
class TestLoans(unittest.TestCase): '''获取项目''' Worksheet_name = "loans" excel, data_list = com_excel_read(Worksheet_name) @classmethod def setUpClass(cls): '''登录普通用户获取token,用户id''' cls.excel.wb = openpyxl.load_workbook(cls.excel.path) # 重新载入workbook params = config.get("PRESET", "loan_params") respone = com_request(TestLoans, "login", params=params) cls.token = "Bearer" + " " + jsonpath(respone, "$..token")[0] def setUp(self): pass @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: respone = com_request(TestLoans, self.Worksheet_name, **kwargs, token=self.token) com_assertEqual(self, respone, eval(kwargs["expect"])) except AssertionError as e: log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()
class TestInfo(unittest.TestCase): '''获取用户信息接口测试''' Worksheet_name = "info" excel, data_list = com_excel_read(Worksheet_name) @classmethod def setUpClass(cls): '''随机获取用户id''' cls.excel.wb = openpyxl.load_workbook(cls.excel.path) # 重新载入workbook sql = "select id from futureloan.member where 1 order by rand() limit 10;" # 截取一个已注册id res = mysql.sql_read(sql) cls.member_id = res["id"] def setUp(self): pass @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: respone = com_request(TestInfo, self.Worksheet_name, **kwargs) com_assertEqual(self, respone, eval(kwargs["expect"])) except AssertionError as e: log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()
class TestAudit(unittest.TestCase): '''审核项目''' Worksheet_name = "audit" excel, data_list = com_excel_read(Worksheet_name) @classmethod def setUpClass(cls): '''登录管理员账户获取token,用户id''' cls.excel.wb = openpyxl.load_workbook(cls.excel.path) # 重新载入workbook # ----------------管理员用户登录------------------------- params = config.get("PRESET", "admin_params") respone = com_request(TestAudit, "login", params=params) cls.admin_token = "Bearer" + " " + jsonpath(respone, "$..token")[0] cls.admin_member_id = jsonpath(respone, "$..id")[0] # ----------------普通用户登录------------------------- params = config.get("PRESET", "loan_params") respone = com_request(TestAudit, "login", params=params) cls.token = "Bearer" + " " + jsonpath(respone, "$..token")[0] cls.member_id = jsonpath(respone, "$..id")[0] def setUp(self): '''新增项目''' expect = {'code': 0, 'msg': 'OK'} # 预期结果转换成字典 params = str({ "member_id": self.member_id, "title": "孙忘_财务自由1", "amount": 1000000000, "loan_rate": 1, "loan_term": 1, "loan_date_type": 1, "bidding_days": 1 }) # 参数处理 respone = com_request(TestAudit, "add", params=params, token=self.token) com_assertEqual(self, respone, expect) TestAudit.loan_id = jsonpath(respone, "$..id")[0] @ddt.data(*data_list) @ddt.unpack def test_case(self, info, **kwargs): '''{info}''' try: respone = com_request(TestAudit, self.Worksheet_name, **kwargs, token=self.admin_token) com_assertEqual(self, respone, eval(kwargs["expect"])) if kwargs["sql_cheack"]: # 存在sql校验,获取审核项目状态 res = mysql.sql_read(kwargs["sql_cheack"], self.loan_id) if self.params["approved_or_not"]: statue = 2 else: statue = 5 print("实际项目状态:", res["status"]) print("预期项目状态:", statue) self.assertEqual(res["status"], statue) # 校验更新后的状态与预期 TestAudit.loan_id_pass = jsonpath(self.params, "$..loan_id")[0] except AssertionError as e: log.error(f"用例--{info}--执行失败", exc_info=True) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "失败") raise e else: log.info(f"用例--{info}--执行成功", exc_info=False) self.excel.excel_write(self.Worksheet_name, kwargs["id"], "成功") def tearDown(self): pass @classmethod def tearDownClass(cls): cls.excel.wb.save(cls.excel.path) cls.excel.wb.close()