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 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 test_car_register(self, test_info): 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"])) expected = json.loads(test_info["expected"]) self.assertTrue(expected["code"] == resp["code"]) self.assertTrue(expected["msg"] == resp["msg"]) logger.info("第{}条测试用例通过".format(test_info["case_id"]))
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_login(self, test_info): expected = json.loads(test_info["expected"]) url = Handler().host + test_info["url"] resp = visit( test_info["method"], url=url, json=json.loads(test_info["data"]), ) try: self.assertEqual(expected["code"], resp["code"]) self.assertEqual(expected["msg"], resp["msg"]) 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"返回的不是json格式:{e}") Handler.excel.write("登录", test_info["case_id"] + 1, 9, "失败") raise e
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 setUpClass(cls): cls.member_id = Handler().member_id
from middlerware.handler import Handler, replace_data
from middlerware.handler import Handler,replace_data
from middlerware.handler import Handler, replace_data, MySqlHandlerWare
from middlerware.handler import Handler, MySqlHandlerWare, replace_data