class TestWithdraw(unittest.TestCase): excel = Excel(os.path.join(DATADIR, "apicases.xlsx"), "withdraw") cases = excel.read_data() request = Requests() db = DB() @data(*cases) def testwithdraw(self, case): #准备测试数据,对登录接口用例替换相应的测试手机号、密码 url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) expected = eval(case["expected"]) row = case["case_id"] + 1 title = case["title"] case["data"] = ReplaceData.replace_data(case["data"]) data = eval(case["data"]) # 判断是否是取现接口,取现接口则加上请求头,替换相应的用户id,添加时间戳和签名到json请求体 if case["interface"] == "withdraw": headers["Authorization"] = ReplaceData.Authorization sign = HandleSign.generate_sign(ReplaceData.token) data.update(sign) # 判断是否需要进行sql校验 if case["sql_check"]: #查询充值前用户的可用余额 start_leave_amount = self.db.find_one(case["sql_check"].format( conf.get("test_data", "phone")))["leave_amount"] #发送请求,获取实际结果 respons = self.request.send(url=url, method=method, headers=headers, json=data) res = respons.json() # 判断是否是登录接口 if case["interface"] == "login": # 提取token,保存为类属性 ReplaceData.token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] ReplaceData.Authorization = token_type + " " + ReplaceData.token # 提取用户id保存为类属性 ReplaceData.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) #断言,比对结果 try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["sql_check"]: end_leave_amount = self.db.find_one(case["sql_check"].format( conf.get("test_data", "phone")))["leave_amount"] self.assertEqual(Decimal(str(data["amount"])), start_leave_amount - end_leave_amount) except AssertionError as e: print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) self.excel.write_data(row=row, column=8, value="未通过") log.error("用例未通过:{},错误原因:{}".format(title, e)) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.debug("用例通过:{}".format(title))
class TestUpdate(unittest.TestCase): excel = Excel(os.path.join(DATADIR, "apicases.xlsx"), "update") cases = excel.read_data() request = Requests() db = DB() @data(*cases) def testupdate(self, case): #准备测试数据,替换用例中相应的手机号、密码、用户id、项目id url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) expected = eval(case["expected"]) row = case["case_id"] + 1 title = case["title"] case["data"] = ReplaceData.replace_data(case["data"]) data = eval(case["data"]) # 判断是否是更新昵称接口,更新昵称接口则加上请求头 if case["interface"] == "update": headers["Authorization"] = ReplaceData.Authorization #添加时间戳和签名到json请求体 sign = HandleSign.generate_sign(ReplaceData.token) data.update(sign) #发送请求,获取实际结果 respons = self.request.send(url=url, method=method, headers=headers, json=data) res = respons.json() #获取用户id和token值,并保存为类属性 if case["interface"] == "login": ReplaceData.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) ReplaceData.token = jsonpath.jsonpath(res, "$..token")[0] ReplaceData.Authorization = jsonpath.jsonpath( res, "$..token_type")[0] + " " + jsonpath.jsonpath( res, "$..token")[0] #断言:比对预期结果和实际结果 try: self.assertEqual(expected["code"], res["code"]) self.assertIn(expected["msg"], res["msg"]) #判断是否需要SQL校验 if case["sql_check"]: #获取执行结果后用户昵称 end_name = self.db.find_one(case["sql_check"].format( ReplaceData.member_id))["reg_name"] self.assertEqual(data["reg_name"], end_name) except AssertionError as e: print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) self.excel.write_data(row=row, column=8, value="未通过") log.error("用例未通过:{},错误原因:{}".format(title, e)) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.debug("用例通过:{}".format(title))
class TestLoans(unittest.TestCase): excel = Excel(os.path.join(DATADIR,"apicases.xlsx"),"loans") cases = excel.read_data() request = Requests() db = DB() @data(*cases) def testloans(self,case): #准备测试数据,替换用例中相应的手机号、密码、用户id、项目id url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) expected = eval(case["expected"]) row = case["case_id"] + 1 title = case["title"] case["data"] = ReplaceData.replace_data(case["data"]) data = eval(case["data"]) #发送请求,获取实际结果 respons = self.request.send(url=url, method=method, headers=headers, params=data,json=data) res = respons.json() #断言:比对预期结果和实际结果 try: self.assertEqual(expected["code"],res["code"]) self.assertIn(expected["msg"],res["msg"]) except AssertionError as e : print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) self.excel.write_data(row=row,column=8,value="未通过") log.error("用例未通过:{},错误原因:{}".format(title,e)) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.debug("用例通过:{}".format(title))
class TestRigster(unittest.TestCase): excel = Excel(os.path.join(DATADIR, "apicases.xlsx"), 'register') cases = excel.read_data() request = Requests() db = DB() def random_phone(self): """ 随机生成手机号的方法 :return: """ while True: phone = "155" for i in range(0, 8): n = random.randint(0, 9) phone += str(n) sql = "SELECT * FROM futureloan.member WHERE mobile_phone={}".format( phone) res_phone = self.db.find_count(sql) if res_phone == 0: break return phone @data(*cases) def testregister(self, case): """注册单元测试用例""" #准备测试数据 url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) #随机生成一个手机号 phone = self.random_phone() case["data"] = case["data"].replace("#phone#", phone) data = eval(case["data"]) expected = eval(case["expected"]) row = case["case_id"] + 1 #实际结果 respons = self.request.send(url=url, method=method, headers=headers, json=data) #断言:比对预期结果和实际结果 try: self.assertEqual(expected["code"], respons.json()["code"]) self.assertEqual(expected["msg"], respons.json()["msg"]) if case["sql_check"]: # 注册后用户的手机号状态 end_mobile_phone = self.db.find_count( "SELECT mobile_phone FROM futureloan.member WHERE mobile_phone={}" .format(data["mobile_phone"])) #对Excel中“sql_check”值为1的用例进行leaveamount断言 self.assertEqual(1, end_mobile_phone) except AssertionError as e: print("预期结果:{}".format(expected)) print("实际结果:{}".format(respons.json())) self.excel.write_data(row=row, column=8, value="未通过") log.error("{}:用例未通过,原因为:{}".format(case["title"], e)) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.debug("{}:用例通过".format(case["title"]))
class TestAdd(unittest.TestCase): excel = Excel(os.path.join(DATADIR, "apicases.xlsx"), "add") cases = excel.read_data() request = Requests() db = DB() @data(*cases) def testadd(self, case): #准备测试数据,对登录接口用例替换相应的手机号、密码 url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) expected = eval(case["expected"]) row = case["case_id"] + 1 title = case["title"] case["data"] = ReplaceData.replace_data(case["data"]) data = eval(case["data"]) # 判断是否是添加项目接口,添加项目接口则加上请求头,替换相应的用户id if case["interface"] == "add": headers["Authorization"] = ReplaceData.Authorization #添加时间戳和签名到json请求体 sign = HandleSign.generate_sign(ReplaceData.token) data.update(sign) #判断是否需要sql校验 if case["sql_check"]: # 查询执行用例前当前用户添加的项目总数 start_loan = self.db.find_count(case["sql_check"].format( ReplaceData.member_id)) #发送请求,获取实际结果 respons = self.request.send(url=url, method=method, headers=headers, json=data) res = respons.json() # 判断是否是登录接口 if case["interface"] == "login": # 提取token,保存为类属性 ReplaceData.token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] ReplaceData.Authorization = token_type + " " + ReplaceData.token # 提取用户id保存为类属性 ReplaceData.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) #断言:比对预期结果和实际结果 try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) if case["sql_check"]: # 查询用例执行后当前用户添加的项目总数 end_loan = self.db.find_count(case["sql_check"].format( ReplaceData.member_id)) self.assertEqual(1, end_loan - start_loan) except AssertionError as e: print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) self.excel.write_data(row=row, column=8, value="未通过") log.error("用例未通过:{},错误原因:{}".format(title, e)) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.debug("用例通过:{}".format(title))
class TestInvest(unittest.TestCase): excel = Excel(os.path.join(DATADIR, "apicases.xlsx"), "invest") cases = excel.read_data() request = Requests() db = DB() @data(*cases) def testinvest(self, case): #准备测试数据,替换用例中相应的手机号、密码、用户id、项目id url = conf.get("env", "url") + case["url"] method = case["method"] headers = eval(conf.get("env", "headers")) expected = eval(case["expected"]) row = case["case_id"] + 1 title = case["title"] case["data"] = ReplaceData.replace_data(case["data"]) data = eval(case["data"]) # 判断是否是投资接口,投资接口则加上请求头 if case["interface"] != "login": headers["Authorization"] = ReplaceData.Authorization # 添加时间戳和签名到json请求体 sign = HandleSign.generate_sign(ReplaceData.token) data.update(sign) #判断是否需要sql校验 if case["sql_check"]: # 判断SQL校验是普通用例还是生成了回款计划的用例 if case["title"] == "投资金额等于项目剩余可投金额": sql = eval(case["sql_check"]) # 查询执行用例前当前用户的可用余额数 start_leave_amount = self.db.find_one(sql[0].format( ReplaceData.member_id))["leave_amount"] # 查询执行用例前该项目中当前用户的投资记录总数 start_invest = self.db.find_count(sql[1].format( ReplaceData.loan_id, ReplaceData.member_id)) # 查询执行用例前当前该项目中当前用户的流水记录总数 start_financelog = self.db.find_count(sql[2].format( ReplaceData.member_id)) # 查询执行用例前当前该项目中当前用户的回款记录总数 start_repayment = self.db.find_count(sql[3].format( ReplaceData.loan_id, ReplaceData.member_id)) else: # 查询执行用例前该项目中当前用户的投资记录总数 start_invest = self.db.find_count(case["sql_check"].format( getattr(ReplaceData, "loan_id"), getattr(ReplaceData, "member_id"))) #发送请求,获取实际结果 respons = self.request.send(url=url, method=method, headers=headers, json=data) res = respons.json() #获取管理员用户id和token值,并保存为类属性 if case["interface"] == "login": ReplaceData.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) ReplaceData.token = jsonpath.jsonpath(res, "$..token")[0] ReplaceData.Authorization = jsonpath.jsonpath( res, "$..token_type")[0] + " " + jsonpath.jsonpath( res, "$..token")[0] # 获取加标项目id,并保存为类属性 if case["interface"] == "add": ReplaceData.loan_id = str(jsonpath.jsonpath(res, "$..id")[0]) #断言:比对预期结果和实际结果 try: self.assertEqual(expected["code"], res["code"]) self.assertIn(expected["msg"], res["msg"]) #判断是否需要SQL校验 if case["sql_check"]: #判断SQL校验是普通用例还是生成了回款计划的用例 if case["title"] == "投资金额等于项目剩余可投金额": sql = eval(case["sql_check"]) # 查询执行用例前当前用户的可用余额数 end_leave_amount = self.db.find_one(sql[0].format( ReplaceData.member_id))["leave_amount"] self.assertEqual( end_leave_amount, start_leave_amount - Decimal(str(data["amount"]))) # 查询执行用例前该项目中当前用户的投资记录总数 end_invest = self.db.find_count(sql[1].format( ReplaceData.loan_id, ReplaceData.member_id)) self.assertEqual(1, end_invest - start_invest) # 查询执行用例前当前该项目中当前用户的流水记录总数 end_financelog = self.db.find_count(sql[2].format( ReplaceData.member_id)) self.assertEqual(1, end_financelog - start_financelog) # 查询执行用例前当前该项目中当前用户的回款记录总数 end_repayment = self.db.find_count(sql[3].format( ReplaceData.loan_id, ReplaceData.member_id)) self.assertEqual(3, end_repayment - start_repayment) else: # 查询执行用例前该项目中当前用户的投资记录总数 end_invest = self.db.find_count(case["sql_check"].format( ReplaceData.loan_id, ReplaceData.member_id)) self.assertEqual(1, end_invest - start_invest) except AssertionError as e: print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) self.excel.write_data(row=row, column=8, value="未通过") log.error("用例未通过:{},错误原因:{}".format(title, e)) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.debug("用例通过:{}".format(title))