标签:write finally == span sql语句 login mysql mem rom
# 8.test_class_auto_incre # 使用ddt import requests import unittest from p2p_project_7.tools.http_request import HttpRequest from p2p_project_7.tools.get_data import GetData from p2p_project_7.tools.do_excel_auto_incre_by_fanshe_2 import DoExcel from p2p_project_7.tools import project_path from p2p_project_7.tools.do_mysql_class import DoMysql from p2p_project_7.tools.write_log_class import MyLog from ddt import ddt, data test_data = DoExcel().get_data(project_path.test_case_path) my_logger = MyLog() @ddt class TestLogin(unittest.TestCase): def setUp(self): print("start testing...") def tearDown(self): print("case end.") @data(*test_data) def test_api(self, item): # print("self.method: {}, type:{}".format(self.method, type(self.method))) # print("self.data: {}".format(self.data)) # print("cookie是:", GetCookie.Cookie) # 替换loan_id my_logger.info("开始执行用例{0}:{1}".format(item["case_id"], item["tittle"])) if item["data"]["id"].find("${loan_id}") != -1: # 在数据库中根据借款人账号找到标id,使用max是因为如果测试账号,可能有多个人同时使用用来加标 query_sql = "select max(Id) from loan where MemberID={0}".format(getattr(GetData, "loan_member_id")) loan_id = DoMysql().do_mysql(query_sql)[0][0] item["data"] = item["data"].replace("${loan_id}", str(loan_id)) # 把标id的值存到反射,也可以不存,不使用反射,每次都直接在数据库中取值; # 使用反射的原因是多个人用同一个账号进行加标,不能保证每次取到的都是自己加的那个标,所以用反射把自己加的标id存起来 setattr(GetData, "LoanId", loan_id) my_logger.info("标id是:{0}".format(loan_id)) else: # loan_id有值的话就直接使用反射进行替换 my_logger.info("标id是:{0}".format(getattr(GetData, "LoanId"))) item["data"] = item["data"].replace("${loan_id}", str(getattr(GetData, "LoanId"))) my_logger.info("获取到的请求数据是:{0}".format(item["data"])) my_logger.info("----------------开始接口请求-----------------") if item["sheet_name"] in getattr(GetData, "check_list"): # 充值请求之前查询余额 # 查询数据库 query_sql = "select LeaveAmount from member where mobile={0}".format(item["data"]["mobile"]) BeforeAmount = DoMysql().do_mysql(query_sql, 1)[0] res = HttpRequest().http_request(item["url"], item["method"], item["data"], cookie=GetData.Cookie) # 充值请求之后校验余额 query_sql = "select LeaveAmount from member where mobile={0}".format(item["data"]["mobile"]) AfterAmount = DoMysql().do_mysql(query_sql, 1)[0] # 61-64 行代码:不是所有的用例都这么检查,因为有的用例不涉及充值,优化做法是在excel中定义好哪些用例需要查余额 if abs(BeforeAmount - AfterAmount) == eval(item["data"])["amount"]: check_res = "金额正确" else: check_res = "金额不对" # print("哪出错了") # print(res.text) r = res.json()["errorCode"] # 如果GetCookie.Cookie没有值,就把响应的cookie值赋给GetCookie.Cookie,以便下次请求使用 if GetData.Cookie is None: setattr(GetData, "Cookie", res.cookies) # 如果GetCookie.Cookie不为空,就把新获得的res.cookies添加到原有的cookie中 else: GetData.Cookie.update(res.cookies) try: self.assertEqual(r, item["expected"]) test_result = "PASS" # 用例成功 except AssertionError as e: test_result = "FAILED" # 用例失败 print("there is an error: {}".format(e)) raise e # 无论是否异常,finally后面都要执行。这里是写入响应结果和测试结论 finally: DoExcel().write_back(project_path.test_case_path, item["sheet_name"], item["case_id"] + 1, str(res.json()), test_result) print(res.json())
配置文件:
测试数据:
对需要检查余额的用例写入SQL语句:
unittest(22)- p2p项目实战(8)-test_class_auto_incre
标签:write finally == span sql语句 login mysql mem rom
原文地址:https://www.cnblogs.com/come202011/p/12232281.html