def test_audit(self, item): url = conf.get('env', 'base_url') + item['url'] item['data'] = replace_data(item['data'], TestAudit) params = eval(item['data']) method = item['method'] expected = eval(item['expected']) response = requests.request(method=method, url=url, json=params, headers=self.admin_headers) res = response.json() # 判断是否审核通过的用例,且审核成功,如果是则保存项目ID为审核通过的项目ID # if res['msg']=='OK' and params['approved_or_not']=='True': if res['msg'] == 'OK' and item['title'] == '审核通过': TestAudit.pass_loan_id = params['loan_id'] # 第三步:断言 print("预期结果:", expected) print("实际结果:", res) try: # 断言code和msg字段是否一致 self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if item['check_sql']: sql = item['check_sql'].format(self.loan_id) status = self.db.find_one(sql)[0] print('数据库中的状态', status) self.assertEqual(expected['status'], status) except AssertionError as e: # 记录日志 my_log.error("用例--【{}】---执行失败".format(item['title'])) my_log.exception(e) raise e else: my_log.info("用例--【{}】---执行通过".format(item['title']))
def test_add(self, item): # 准备数据 url = conf.get('env', 'base_url') + item['url'] item['data'] = replace_data(item['data'], TestAdd) params = eval(item['data']) expected = eval(item['expected']) method = item['method'] sql = 'select * from futureloan.loan where member_id={}'.format( self.member_id) start_count = self.db.find_count(sql) print('调用之前项目个数', start_count) response = requests.request(method=method, url=url, json=params, headers=self.headers) res = response.json() end_count = self.db.find_count(sql) print('调用之后项目个数', end_count) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) #根据添加项目是否成功,来对数据库进行分别校验 if res['msg'] == 'OK': self.assertEqual(end_count - start_count, 1) else: self.assertEqual(end_count - start_count, 0) except AssertionError as e: my_log.error('用例--【{}】---执行失败'.format(item['title'])) my_log.exception(e) raise e else: my_log.info('用例--【{}】---执行成功'.format(item['title']))
def test_01(self, item): # 准备数据 method = item['method'] url = d1.get('url', 'base_url') + item['url'] # 动态变更参数,字符串替换参数 params = eval(replace_data(item['data'], TestAdd)) expected = eval(item['expected']) # 获取实际结果 res = requests.request(method=method, url=url, json=params, headers=self.headers) print('预期结果:', expected) print('实际结果:', res.json()) # 断言 try: self.assertEqual(expected['code'], res.json()['code']) self.assertEqual(expected['msg'], res.json()['msg']) except AssertionError as e: my_log.error(f'用例执行--{item["title"]}--失败') raise e else: my_log.info(f'用例执行--{item["title"]}--成功')
def test_invest(self, item): url = conf.get('env', 'base_url') + item['url'] item['data'] = replace_data(item['data'], TestInvest) params = eval(item['data']) expected = eval(item['expected']) method = item['method'] # ----------------投资前查询数据库---------------------------- #查用户表的sql sql1 = 'select leave_amount FROM futureloan.member where id= "{}"'.format( self.member_id) # 查投资记录的sql sql2 = 'select id FROM futureloan.invest where member_id= "{}"'.format( self.member_id) # 查流水记录的sql sql3 = 'select id FROM futureloan.financelog where pay_member_id= "{}" '.format( self.member_id) if item['check_sql']: s_amount = self.db.find_one(sql1)[0] s_invest = self.db.find_count(sql2) s_financelog = self.db.find_count(sql3) response = requests.request(method=method, url=url, json=params, headers=self.headers) res = response.json() # --------------投资后查询数据库----------------------------- if item['check_sql']: e_amount = self.db.find_one(sql1)[0] e_invest = self.db.find_count(sql2) e_financelog = self.db.find_count(sql3) try: self.assertEqual(expected['code'], res['code']) self.assertIn(expected['msg'], res['msg']) if item['check_sql']: # 断言用户余额 self.assertEqual(params['amount'], float(s_amount - e_amount)) # 断言投资记录 self.assertEqual(1, e_invest - s_invest) # 断言流水记录 self.assertEqual(1, e_financelog - s_financelog) except AssertionError as e: my_log.error("用例--【{}】---执行失败".format(item['title'])) my_log.exception(e) raise e else: my_log.info("用例--【{}】---执行通过".format(item['title']))
def test_withdraw(self, item): # 第一步准备数据 url = conf.get('env', 'base_url') + item['url'] # *******************************动态替换参数*************************************** # 动态处理需要替换的参数 # item['data'] = item['data'].replace('#menber_id#', str(self.member_id)) # print(item['data']) item['data'] = replace_data(item['data'], TestWithdraw) params = eval(item['data']) # ******************************************************************************* expected = eval(item['expected']) method = item['method'].lower() # &&&&&&&&&&&&&&取现接口之前查询用户的余额&&&&&&&&&&&&&&&&&&&& sql = 'select leave_amount FROM futureloan.member WHERE mobile_phone="{}"'.format( conf.get('test_data', 'mobile')) start_amount = self.db.find_one(sql)[0] print('用例执行前的余额', start_amount) # 第二步发送请求,获取接口的实际返回结果 response = requests.request(method=method, url=url, json=params, headers=self.headers) res = response.json() print("预期结果:", expected) print("实际结果:", res) # &&&&&&&&&&&&&&取现之后查询用户的余额&&&&&&&&&&&&&& end_amount = self.db.find_one(sql)[0] print('用例执行后的余额', end_amount) # 第三步断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if item['check_sql']: self.assertEqual(params['amount'], float(start_amount - end_amount)) except AssertionError as e: my_log.error('用例--【{}】--失败'.format('title')) my_log.exception(e) raise e else: my_log.info('用例--【{}】--成功'.format('title'))
def test_recharge(self, item): # 第一步准备数据 url = conf.get('env', 'base_url') + item['url'] # *******************************动态替换参数*************************************** # 动态处理需要替换的参数 # item['data'] = item['data'].replace('#member_id#', str(self.member_id)) # print(item['data']) item['data'] = replace_data(item['data'], TestRecharge) params = eval(item['data']) # ******************************************************************************* expected = eval(item['expected']) method = item['method'].lower() # 第二步发送请求,获取接口的实际返回结果 # &&&&&&&&&&&&&&请求接口之前查询用户的余额&&&&&&&&&&&&&&&&&&&& sql = 'select leave_amount FROM futureloan.member WHERE mobile_phone="{}"'.format( conf.get('test_data', 'mobile')) start_amount = self.db.find_one(sql)[0] print('用例执行前的余额', start_amount) response = requests.request(method=method, url=url, json=params, headers=self.headers) res = response.json() print('预期结果', expected) print('实际结果', res) # &&&&&&&&&&&&&&请求接口之后查询用户的余额&&&&&&&&&&&&&& end_amount = self.db.find_one(sql)[0] print('用例执行后的余额', end_amount) # 第三步断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) # &&&&&&&&&&&&&&校验数据库中用户金额的变化是否等于充值金额&&&&&&&&&&&&&&# if res['msg'] == 'OK': # 充值成功,用户余额的变化为充值金额 self.assertEqual(float(end_amount - start_amount), params['amount']) else: # 充值失败用户余额变化为0 self.assertEqual(float(end_amount - start_amount), 0) except AssertionError as e: my_log.error('用例--【{}】--失败'.format('title')) my_log.exception(e) raise e else: my_log.info('用例--【{}】--成功'.format('title'))
def test_register(self, item): # 第一步准备用例数据 # 接口地址 url = self.base_url + item['url'] # phone = self.random_mobile() # item['data'] = item['data'].replace('#mobile#', phone) # # 接口请求参数 # 判斷是否有手机号需要替换 if '#mobile#' in item['data']: # phone = self.random_mobile() setattr(TestRegister,'mobile',self.random_mobile()) item['data'] = replace_data( item['data'], TestRegister) params = eval(item['data']) # 请求头 # 获取请求方法并转换为小写 method = item['method'].lower() # 用例预期结果 expected = eval(item['expected']) # 第二步请求接口,获取实际结果 # requests.post(url=url,json=params,headers=self.headers) response = requests.request(method, url, json=params, headers=self.headers) res = response.json() # 查询该数据库中该手机号对应的账户数量 sql = 'select * FROM futureloan.member WHERE mobile_phone="{}"'.format(params.get('mobile_phone', "")) count = self.db.find_count(sql) # 第三步 断言 print('预期结果', expected) print('实际结果', res) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if item['check_sql']: print('数据库中查询的数量为:', count) self.assertEqual(1, count) except AssertionError as e: my_log.error('用例---【{}】---执行失败'.format(item['title'])) my_log.exception(e) # my_log.error(e) # 回写结果到Excel raise e else: my_log.info('用例---【{}】---执行通过'.format(item['title']))
def test_01(self, item): # 准备数据 method = item['method'] url = d1.get('url', 'base_url') + item['url'] # 动态变更参数,字符串替换参数 # params = eval(item['data'].replace('#member_id#', self.member_id)) params = eval(replace_data(item['data'], TestCharge)) headers = self.headers expected = eval(item['expected']) # ------------------数据库获取充值前的金额--------------------------- sql = f'select leave_amount from futureloan.member where mobile_phone={d1.get("login", "mobile_phone")};' exc_sql = self.db.find_one(sql)[0] # 获取实际结果 res = requests.request(method=method, url=url, json=params, headers=headers) print('预期结果:', expected) print('实际结果:', res.json()) # ------------------数据库获取充值后的金额--------------------------- res_sql = self.db.find_one(sql)[0] print('充值前金额:', exc_sql) print('充值后金额:', res_sql) # 断言 try: self.assertEqual(expected['code'], res.json()['code']) self.assertEqual(expected['msg'], res.json()['msg']) # ------------------断言数据库充值金额------------------ if item['check_sql'] == 1: self.assertEqual(float(params['amount']), float(res_sql) - float(exc_sql)) except AssertionError as e: my_log.error(f'用例执行--{item["title"]}--失败') raise e else: my_log.info(f'用例执行--{item["title"]}--成功')
def test_login(self, item): url = self.base_url + item['url'] item['data'] = replace_data(item['data'], TestLogin) params = eval(item['data']) method = item['method'].lower() expected = eval(item['expected']) response = requests.request(method, url, json=params, headers=self.headers) res = response.json() try: # self.assertEqual(expected['code'], res['code']) # self.assertEqual(expected['msg'], res['msg']) self.assertDictIn(expected, res) except AssertionError as e: my_log.error('用例---【{}】---执行失败'.format(item['title'])) # my_log.exception(e) raise e else: my_log.info('用例---【{}】---执行通过'.format(item['title']))
def test_add(self, item): # 准备数据 url = conf.get('env', 'base_url') + item['url'] item['data'] = replace_data(item['data'], TestAdd) params = eval(item['data']) expected = eval(item['expected']) method = item['method'] response = requests.request(method=method, url=url, json=params, headers=self.headers) res = response.json() try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: my_log.error('用例--【{}】---执行失败'.format(item['title'])) my_log.exception(e) raise e else: my_log.info('用例--【{}】---执行成功'.format(item['title']))