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"]))
def test_user(self, case): url = conf.get("env", "url") + replace_data(case["url"]) method = case["method"] headers = eval(conf.get("env", "headers")) headers["Authorization"] = getattr(CaseData, "token_value") expected = eval(case["expected"]) row = case["case_id"] + 1 response = self.request.send_request(url=url, method=method, headers=headers) res = response.json() user_id = jsonpath.jsonpath(res, "$..id")[0] CaseData.user_id = str(user_id) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["check_sql"]: sql = replace_data(case["check_sql"]) user_phone = self.db.find_one(sql)["mobile_phone"] self.assertEqual(conf.get("test_data", "phone"), user_phone) 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"]))
def test_loans(self, case): url = conf.get("env", "url") + case["url"] method = case["method"] 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, params=data) res = response.json() count = jsonpath.jsonpath(res, "$..data")[0] count1 = len(count) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["title"] != "不传参数": self.assertEqual(data["pageSize"], count1) 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"]))
def setUp(self): url = conf.get( "env", "url" ) + "/loan/add" data = eval( replace_data( conf.get( "test_data", "data" ) ) ) headers = eval( conf.get( "env", "headers" ) ) headers["Authorization"] = getattr( CaseData, "admin_token_value" ) response = self.request.send_request( url=url, headers=headers, method="post", json=data ) res = response.json() loan_id = jsonpath.jsonpath( res, "$..id" )[0] CaseData.loan_id = str( loan_id )
def __init__(self): #创建一个对象 self.conn = pymysql.connect(host=conf.get("mysql", "host"), port=conf.getint("mysql", "port"), user=conf.get("mysql", "user"), password=conf.get("mysql", "pwd"), charset=conf.get("mysql", "charset"), cursorclass=pymysql.cursors.DictCursor) #创建一个游标 self.cur = self.conn.cursor()
def setupClass(cls): url = conf.get( "env", "url" ) + "/member/login" headers = eval( conf.get( "env", "headers" ) ) data = { "mobile_phone": conf.get( "test_data", "admin_phone" ), "pwd": conf.get( "test_data", "admin_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.admin_member_id = str( member_id ) CaseData.admin_token_value = token_type + " " + token
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"]))
def create_log(): #创建收集器,设置收集器等级 mylog = logging.getLogger(conf.get("log", "name")) mylog.setLevel(conf.get("log", "level")) #创建输出到控制台,设置等级 sh = logging.StreamHandler() sh.setLevel(conf.get("log", "sh_level")) mylog.addHandler(sh) #创建输出到文件,设置等级 fh = logging.FileHandler(filename=os.path.join(LOGDIR, "log.log"), encoding="utf-8") fh.setLevel(conf.get("log", "fh_level")) mylog.addHandler(fh) #设置日志输出格式 formater = '%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s' fm = logging.Formatter(formater) sh.setFormatter(fm) fh.setFormatter(fm) return mylog
def replace_data(s): r1 = r"#(.+?)#" while re.search(r1, s): res = re.search(r1, s) data = res.group() key = res.group(1) try: # s = re.sub(r1,conf.get("test_data",key),s,1) s = s.replace(data, conf.get("test_data", key)) except Exception: s = s.replace(data, getattr(CaseData, key)) return s
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])
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"]))
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 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"]))
def send_email(file_name, title): #第一步:链接邮箱的smtp服务器,并登陆 smtp = smtplib.SMTP_SSL(host=conf.get("email", "host"), port=conf.getint("email", "port")) smtp.login(user=conf.get("email", "user"), password=conf.get("email", "pwd")) #第二步:构建一封邮件 #创建一封多组件的邮件 msg = MIMEMultipart() with open(file_name, "rb") as f: content = f.read() # 创建邮件文本内容 text_msg = MIMEText(content, _subtype="html", _charset="utf8") #添加到多组件的邮件中 msg.attach(text_msg) #创建邮件的附件 report_file = MIMEApplication(content) #截取文件名 (path, file_name1) = os.path.split(file_name) report_file.add_header('content-disposition', 'attachment', filename=file_name1) #将附件添加到多组件邮件中 msg.attach(report_file) # 发件主题 msg["Subject"] = title #发件人 msg["From"] = conf.get("email", "from_addr") #收件人 msg["To"] = conf.get("email", "to_addr") # 第三步发送邮件 smtp.send_message(msg, from_addr=conf.get("email", "from_addr"), to_addrs=conf.get("email", "to_addr"))
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"]))