class Test_Loans(unittest.TestCase): excel = operation_excel(case_file, "loans") cases = excel.read_excel() request = SendRequests() @data(*cases) def test_loans(self, case): url = conf.get("env", "url") + case["url"] method = case["method"] data = replace_data(case['data']) headers = eval(conf.get("env", "headers")) expected = eval(case['expected']) row = case['case_id'] + 1 response = self.request.send_requests_sc(url=url,method=method,params=data,headers=headers) res = response.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
class Test_register(unittest.TestCase): excel = operation_excel(case_file, 'register') cases = excel.read_excel() request = SendRequests() db = DB() @data(*cases) def test_register(self, case): # 准备用例数据 url = conf.get("env", "url") + case['url'] method = case['method'] # 替换手机号码 phone = self.iphone() case['data'] = case['data'].replace('#phone#', phone) data = eval(case["data"]) headers = eval(conf.get('env', 'headers')) # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 注册后,进行数据库校验 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) # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过')) # 手机号码 def iphone(self): phone = '138' for i in range(8): n = random.randint(1, 9) phone += str(n) return phone
class Test_Invest(unittest.TestCase): excel = operation_excel(case_file, 'invest') cases = excel.read_excel() request = SendRequests() @data(*cases) def test_invest(self, case): # 准备用例数据 url = conf.get("env", "url") + case["url"] method = case['method'] case["data"] = replace_data(case["data"]) data = eval(case["data"]) headers = eval(conf.get('env', 'headers')) # 判断当前接口是不是登录,加token if case["interface"] != "login": headers["Authorization"] = getattr(Case_Data,"token_value") # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 判断发送请求之后,判断是不是登录,提取token if case["interface"].lower() == "login": Case_Data.member_id = str(jsonpath.jsonpath(res, '$..id')[0]) token = jsonpath.jsonpath(res, '$..token')[0] token_type = jsonpath.jsonpath(res, '$..token_type')[0] # 提取token保存类属性 Case_Data.token_value = token_type + " " + token # 判断是否是加标的用例,就加标id if case["interface"] == "add": Case_Data.loan_id = str(jsonpath.jsonpath(res,"$..id")[0]) # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertIn(expected["msg"],res["msg"]) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
class Test_Update(unittest.TestCase): excel = operation_excel(case_file,"update") cases = excel.read_excel() request = SendRequests() @classmethod def setUpClass(cls): 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_requests_sc(url=url, method="post", json=data, headers=headers) res = response.json() token = jsonpath.jsonpath(res,"$..token")[0] token_type = jsonpath.jsonpath(res,"$..token_type")[0] Case_Data.token_value = token_type + " " + token Case_Data.member_id = str(jsonpath.jsonpath(res,"$..id")[0]) @data(*cases) def test_update(self,case): url = conf.get('env','url') + case["url"] method =case['method'] data = eval(replace_data(case["data"])) headers =eval(conf.get('env','headers')) headers["Authorization"] = getattr(Case_Data, "token_value") expected = eval(case['expected']) row = case['case_id'] + 1 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
class Test_login(unittest.TestCase): excel = operation_excel(case_file, 'login') cases = excel.read_excel() request = SendRequests() @data(*cases) def test_login(self, case): # 准备用例数据 url = conf.get("env", "url") + case["url"] method = case['method'] data = eval(case["data"]) headers = eval(conf.get('env', 'headers')) # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
class Test_Info(unittest.TestCase): excel = operation_excel(case_file, 'info') cases = excel.read_excel() request = SendRequests() db = DB() @classmethod def setUpClass(cls): # 准备登录的数据 # 获取登录的URL地址 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_requests_sc(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设置为类属性 Case_Data.token_value = token_type + " " + token # 提取用户的登录id,保存为类属性 Case_Data.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) @data(*cases) def test_info(self, case): # 准备用例数据 url = conf.get("env", "url") + replace_data(case["url"]) method = case['method'] headers = eval(conf.get('env', 'headers')) headers["Authorization"] = getattr(Case_Data, "token_value") # 预期结果 expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求获取结果 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 断言比对预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))
class Test_add(unittest.TestCase): excel = operation_excel(file_name, 'add') cases = excel.read_excel() request = SendRequests() @classmethod def setUp(cls): """登录管理员账号""" url = conf.get("env", "url") + "/member/login" data = { "mobile_phone": conf.get("test_data", "admin_phone"), "pwd": conf.get("test_data", "admin_pwd") } # 请求头 headers = eval(conf.get("env", "headers")) response = cls.request.send_requests_sc(url=url, method="post", json=data, headers=headers) res = response.json() token = jsonpath.jsonpath(res, "$..token")[0] token_type = jsonpath.jsonpath(res, "$..token_type")[0] member_id = str(jsonpath.jsonpath(res, "$..id")[0]) # 将提取的数据保存到Case_data属性中 Case_Data.admin_token_value = token_type + " " + token Case_Data.admin_member_id = member_id @data(*cases) def test_add(self, case): # 准备数据 url = conf.get("env", "url") + case["url"] headers = eval(conf.get("env", "headers")) headers["Authorization"] = getattr(Case_Data, "admin_token_value") data = eval(replace_data(case["data"])) expected = eval(case["expected"]) method = case["method"] row = case["case_id"] + 1 # 发送请求 response = self.request.send_requests_sc(url=url, method=method, json=data, headers=headers) res = response.json() # 断言结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过')) pass
class Test_Recharge(unittest.TestCase): excel = operation_excel(case_file, 'recharge') cases = excel.read_excel() request = SendRequests() db = DB() @classmethod def setUpClass(cls): # 准备登录的数据 # 获取登录的URL地址 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_requests_sc(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设置为类属性 Case_Data.token_value = token_type + " " + token # 提取用户的登录id,保存为类属性 Case_Data.member_id = str(jsonpath.jsonpath(res, "$..id")[0]) @data(*cases) 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(Case_Data, "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_requests_sc(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: self.excel.write_excel(row=row, column=8, value='未通过') log.error('用例:{},执行'.format(case['title']) + color.white_red('未通过')) raise e else: self.excel.write_excel(row=row, column=8, value='通过') log.info('用例:{},执行'.format(case['title']) + color.white_green('通过'))