class TestWithdraw(unittest.TestCase): # 下面的方法只执行一次 @classmethod def setUpClass(cls): cls.token = Handler().token cls.member_id = Handler().member_id cls.other_member_id = Handler().other_member_id cls.db = MySqlHandlerWare() sql = "SELECT leave_amount FROM member WHERE id = {};".format(cls.member_id) leave_amount_dict = cls.db.query(sql) leave_money = leave_amount_dict["leave_amount"] if leave_money < 501999.22 or leave_money >= 503500.22: cls.db.update("UPDATE member SET leave_amount = 501999.22 WHERE id = {};".format(cls.member_id)) if leave_money < 1000: cls.db.update("UPDATE member SET leave_amount = 501999.22 WHERE id = {};".format(cls.other_member_id)) # 前置条件、 def setUp(self): self.db = MySqlHandlerWare() def tearDown(self): self.db.close() @ddt.data(*withdraw_data) def test_withdraw(self, test_info): if "#token#" in test_info["headers"]: test_info["headers"] = test_info["headers"].replace("#token#", self.token) if "#member_id#" in test_info["data"]: test_info["data"] = test_info["data"].replace("#member_id#", str(self.member_id)) if "#other_member_id#" in test_info["data"]: test_info["data"] = test_info["data"].replace("#other_member_id#", str(self.other_member_id)) url = Handler.host + test_info["url"] sql = "select * from member where id = {};".format(self.member_id) before_data = self.db.query(sql) before_money = before_data["leave_amount"] resp = visit(method=test_info["method"], url=url, json=eval(test_info["data"]), headers=json.loads(test_info["headers"])) try: for key, value in eval(test_info["expected"]).items(): self.assertTrue(resp[key] == value) if resp["code"] == 0: # 查询之后的余额 sql = "select * from member where id = {};".format(self.member_id) after_data = self.db.query(sql) after_money = after_data["leave_amount"] data = json.loads(test_info["data"])["amount"] new_money = before_money - Decimal(str(data)) # print(new_money) self.assertTrue(new_money == after_money) logger.info("第{}条测试用例通过".format(test_info["case_id"])) except Exception as e: raise e
def random_phone(self): while True: end_num = "".join( random.sample( ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], 8)) phone_number = "158" + end_num phone_data = MySqlHandlerWare().query( "select * from member where mobile_phone = {};".format( phone_number)) if not phone_data: # db_con.close() return phone_number MySqlHandlerWare().close()
def test_login(self, test_info): # phone = self.random_phone() # phone_yes = self.phone_yes() if "#phone#" in test_info["data"]: test_info["data"] = test_info["data"].replace( "#phone#", self.phone) if "#yes_phone#" in test_info["data"]: test_info["data"] = test_info["data"].replace( "#yes_phone#", self.phone_yes) data = json.loads(test_info["data"]) url = Handler().host + test_info["url"] resp = visit(test_info["method"], url=url, json=json.loads(test_info["data"]), headers=json.loads(test_info["headers"])) try: for k, v in json.loads(test_info["expected"]).items(): self.assertEqual(resp[k], v) if resp[k] == 0: db_data = MySqlHandlerWare().query( "select * from member where mobile_phone = {};".format( data["mobile_phone"])) self.assertTrue(db_data) Handler.excel.write("登录", test_info["case_id"] + 1, 9, "通过") logger.info("第{}条测试用例通过".format(test_info["case_id"])) except Exception as e: logger.info("第{}条测试用例失败".format(test_info["case_id"])) print(f"返回的不是jsong格式:{e}") Handler.excel.write("登录", test_info["case_id"] + 1, 9, "失败") raise e
def setUpClass(cls): cls.token = Handler().token cls.member_id = Handler().member_id cls.other_member_id = Handler().other_member_id cls.db = MySqlHandlerWare() sql = "SELECT leave_amount FROM member WHERE id = {};".format(cls.member_id) leave_amount_dict = cls.db.query(sql) leave_money = leave_amount_dict["leave_amount"] if leave_money < 501999.22 or leave_money >= 503500.22: cls.db.update("UPDATE member SET leave_amount = 501999.22 WHERE id = {};".format(cls.member_id)) if leave_money < 1000: cls.db.update("UPDATE member SET leave_amount = 501999.22 WHERE id = {};".format(cls.other_member_id))
class TestAdd(unittest.TestCase): @classmethod def setUpClass(cls): cls.member_id = Handler().member_id def setUp(self): self.db = MySqlHandlerWare() sql = "select * from loan where member_id = {}".format(self.member_id) self.before_loan = self.db.query(sql, one=False) def tearDown(self): self.db.close() @ddt.data(*add_data) def test_add_item(self, test_info): url = Handler.host + test_info["url"] test_info["data"] = replace_data(Handler(), test_info["data"]) test_info["headers"] = replace_data(Handler(), test_info["headers"]) resp = visit(method=test_info["method"], url=url, json=json.loads(test_info["data"]), headers=json.loads(test_info["headers"])) try: expected = json.loads(test_info["expected"]) self.assertTrue(expected["code"] == resp["code"]) self.assertTrue(expected["msg"] == resp["msg"]) if resp["code"] == 0: sql = "select * from loan where member_id = {}".format(self.member_id) after_loan = self.db.query(sql, one=False) self.assertTrue(len(self.before_loan) + 1 == len(after_loan)) logger.info("第{}条测试用例通过".format(test_info["case_id"])) except Exception as e: logger.error("请求不是json格式") raise e
def test_audit(self, test_info): test_info["headers"] = replace_data(Handler(), test_info["headers"]) test_info["data"] = replace_data(Handler(), test_info["data"]) resp = visit(method=test_info["method"], url=Handler.host + test_info["url"], json=json.loads(test_info["data"]), headers=eval(test_info["headers"])) expected = eval(test_info["expected"]) self.assertTrue(expected["code"] == resp["code"]) self.assertTrue(expected["msg"] == resp["msg"]) if resp["code"] == 0: # 验证数据库状态 data = json.loads(test_info["data"]) loan = MySqlHandlerWare().query( "SELECT * FROM loan WHERE id={}".format(data["loan_id"])) self.assertEqual(expected["status"], loan["status"]) logger.info("第{}条测试用例通过".format(test_info["case_id"]))
def test_register(self, test_info): if "#phone#" in test_info["data"]: test_info["data"] = (test_info["data"]).replace( "#phone#", self.phone) data = json.loads(test_info["data"]) url = Handler().host + test_info["url"] resp = visit(test_info["method"], url, json=json.loads(test_info["data"]), headers=json.loads(test_info["headers"])) try: for key, value in json.loads(test_info["expected"]).items(): self.assertEqual(resp[key], value) if resp["code"] == 0: sql = "select * from member where mobile_phone = {};".format( data["mobile_phone"]) phone_data = MySqlHandlerWare().query(sql) self.assertTrue(phone_data) logger.info("第{}条测试用例通过".format(test_info["case_id"])) except Exception as e: print(f"返回的数据不是json格式:{e}") raise e
def setUp(self): self.db = MySqlHandlerWare() sql = "select * from loan where member_id = {}".format(self.member_id) self.before_loan = self.db.query(sql, one=False)
def phone_yes(self): phone_data = MySqlHandlerWare().query( "select mobile_phone from member;") return phone_data["mobile_phone"]
def setUp(self): self.db = MySqlHandlerWare()
from middlerware.handler import Handler, replace_data, MySqlHandlerWare
from middlerware.handler import Handler, MySqlHandlerWare, replace_data