def test_login(self, case): # 准备测试数据 url = conf.get('env', 'url') + case['url'] method = case['method'] if '#phone#' in case['data']: case['data'] = case['data'].replace('#phone#', conf.get('env', 'phone')) data = eval(case['data']) expected = eval(case['expected']) headers = eval(conf.get('env', 'headers')) row = case['case_id'] + 1 # 发送请求 response = self.http.send(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='未通过') mylog.info('用例:{}---->执行未通过'.format(case['title'])) mylog.error(e) print('预期结果:{}'.format(expected)) print('实际结果:{}'.format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='通过') mylog.info('用例:{}---->执行已通过'.format(case['title']))
def test_invest(self,case): pass # 准备用例数据 url=conf.get('env','url')+case['url'] # 测试数据 case['data']=replace_data(case['data']) data=eval(case['data']) # 请求方法 method=case['method'] # 请求头 headers = eval(conf.get('env', 'headers')) if case['interface']!='login': headers['Authorization']=getattr(CaseData,'token_data') # 预期结果 expected=eval(case['expected']) # 行号 row=case['case_id']+1 # 发送请求 response=self.http.send(url=url,method=method,json=data,headers=headers) res=response.json() #提取member_id和token if case['interface']=='login' and res['msg']=='OK': member_id=jsonpath.jsonpath(res,'$..id')[0] setattr(CaseData,'member_id',str(member_id)) token_type=jsonpath.jsonpath(res,'$..token_type')[0] token = jsonpath.jsonpath(res, '$..token')[0] token_data=token_type+' '+token setattr(CaseData,'token_data',token_data) #提取项目id elif case['interface']=='add'and res['msg']: loan_id=jsonpath.jsonpath(res,'$..id')[0] setattr(CaseData,'loan_id',str(loan_id)) # 断言 try: self.assertEqual(expected['code'],res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql=replace_data(case['check_sql']) status=self.db.get_one(sql)[0] self.assertEqual(expected['status'],status) except AssertionError as e: self.excel.write_excel(row=row,column=8,value='未通过') mylog.info('用例:{}---->执行未通过'.format(case['title'])) mylog.error(e) print('预期结果:{}'.format(expected)) print('实际结果:{}'.format(res)) raise e else: self.excel.write_excel(row=row,column=8,value='通过') mylog.info('用例:{}---->执行已通过'.format(case['title']))
def test_add(self,case): # 准备测试数据 url=conf.get('env','url')+case['url'] case['data']=replace_data(case['data']) data=eval(case['data']) headers=eval(conf.get('env','headers')) if case['interface'] !='登录': headers['Authorization']=getattr(CaseData,'token_data') method=case['method'] expected=eval(case['expected']) row=case['case_id']+1 # 请求前查看该用户有几条标记录 if case['check_sql']: sql=case['check_sql'].format(getattr(CaseData,'admin_member_id')) before_count=self.db.count(sql) # 发送请求 response=self.http.send(url=url,method=method,json=data,headers=headers) res=response.json() # 如果是登录接口,提取出id和token值 if case['interface']=='登录': id=jsonpath.jsonpath(res,'$..id')[0] setattr(CaseData,'admin_member_id',str(id)) token_type=jsonpath.jsonpath(res,'$..token_type')[0] token = jsonpath.jsonpath(res, '$..token')[0] token_data=token_type+' '+token setattr(CaseData,'token_data',token_data) # 断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql =case['check_sql'].format(getattr(CaseData,'admin_member_id')) after_count = self.db.count(sql) self.assertEqual((after_count-before_count),1) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') mylog.info('用例:{}---->测试未通过'.format(case['title'])) print('预期结果:{}'.format(expected)) print('实际结果:{}'.format(res)) mylog.error(e) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') mylog.info('用例:{}---->测试已通过'.format(case['title']))
def test_withdraw(self, case): # 准备测试数据 withdraw_url = conf.get('env', 'url') + case['url'] case['data']=replace_data(case['data']) withdraw_data = eval(case['data']) method = case['method'] headers = eval(conf.get('env', 'headers')) if case['interface'] !='登录': headers['Authorization'] =CaseData.token_data row = case['case_id'] + 1 expected=eval(case['expected']) # 发送请求前查询账号余额 if case['check_sql']: sql =case['check_sql'].format(conf.get('env','phone')) before_amount = self.db.get_one(sql)[0] # 发送请求,获取响应结果 response = self.http.send(url=withdraw_url, method=method, json=withdraw_data, headers=headers) res = response.json() # 判断是登录接口还是提现接口 if case['interface'] == '登录': member_id = jsonpath.jsonpath(res, '$..id')[0] token_type = jsonpath.jsonpath(res, '$..token_type')[0] token = jsonpath.jsonpath(res, '$..token')[0] token_data = token_type + ' ' + token setattr(CaseData, 'member_id', str(member_id)) setattr(CaseData,'token_data',token_data) # 比对预期结果与实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql =case['check_sql'].format(conf.get('env','phone')) after_amount = self.db.get_one(sql)[0] self.assertEqual((before_amount-after_amount),Decimal(str(withdraw_data['amount']))) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') mylog.info('用例:{}---->测试未通过'.format(case['title'])) print('预期结果:{}'.format(expected)) print('实际结果:{}'.format(res)) mylog.error(e) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') mylog.info('用例:{}---->测试已通过'.format(case['title']))
def test_audit(self,case): # 准备测试数据 # 准备url地址 url=conf.get('env','url')+case['url'] # 请求方法 method=case['method'] # 请求数据 case['data']=replace_data(case['data']) data=eval(case['data']) print('data:',data) #配置文件获取headers headers=eval(conf.get('env','headers')) headers['Authorization']=getattr(CaseData,'token_data') # 预期结果 expected=eval(case['expected']) row=case['case_id']+1 # 发送请求 response=self.http.send(url=url,method=method,json=data,headers=headers) res=response.json() print('audit_res:',res) if case['title']=='审核通过' and res['msg']=='OK': pass_loan_id=getattr(CaseData,'loan_id') setattr(CaseData,'pass_loan_id',pass_loan_id) # 断言 try: self.assertEqual(expected['code'],res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql=replace_data(case['check_sql']) status=self.db.get_one(sql)[0] self.assertEqual(expected['status'],status) except AssertionError as e: self.excel.write_excel(row=row,column=8,value='未通过') mylog.info('用例:{}---->执行未通过'.format(case['title'])) mylog.error(e) print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row,column=8,value='已通过') mylog.info('用例:{}---->执行已通过'.format(case['title']))
def test_login(self, case): # 准备测试数据 login_data = eval(case['data']) expected = eval(case['expected']) row = case['case_id'] + 1 # 执行功能函数 result = login_check(*login_data) # 比对预期结果与实际结果 try: self.assertEqual(expected, result) except AssertionError as e: self.excel.write_excel(row=row, column=5, value='未通过') mylog.info('用例{}---->测试未通过'.format(case['title'])) mylog.error(e) raise e else: self.excel.write_excel(row=row, column=5, value='通过') mylog.info('用例{}---->测试已通过'.format(case['title']))
def test_recharge(self, case): # 准备测试数据 recharge_url = conf.get('env', 'url') + case['url'] case['data'] = replace_data(case['data']) recharge_data = eval(case['data']) method = case['method'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(CaseData, 'token_data') expected = eval(case['expected']) row = case['case_id'] + 1 # 发送请求前查询一下账号余额 if case['check_sql']: sql = case['check_sql'].format(conf.get('env', 'phone')) before_amount = self.db.get_one(sql)[0] # 发送请求,获取响应结果 response = self.http.send(url=recharge_url, method=method, json=recharge_data, headers=headers) res = response.json() #比对预期结果与实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: sql = case['check_sql'].format(conf.get('env', 'phone')) after_amount = self.db.get_one(sql)[0] self.assertEqual((after_amount - before_amount), Decimal(str(recharge_data['amount']))) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') mylog.info('用例:{}---->测试未通过'.format(case['title'])) print('预期结果:{}'.format(expected)) print('实际结果:{}'.format(res)) mylog.error(e) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') mylog.info('用例:{}---->测试已通过'.format(case['title']))
def test_register(self, case): # 准备测试数据 # 拼接完整的接口地址 url = conf.get('env', 'url') + '/member/register' # 请求方法 method = case['method'] if '#phone#' in case['data']: phone = self.random_phone() case['data'] = case['data'].replace('#phone#', phone) data = eval(case['data']) expected = eval(case['expected']) headers = eval(conf.get('env', 'headers')) row = case['case_id'] + 1 # 2、发送请求 response = self.http.send(url=url, method=method, json=data, headers=headers) res = response.json() # 3、比对预期结果与实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: print(case['check_sql'],type(case['check_sql'])) sql=case['check_sql'].format(conf.get('env','phone')) count=self.db.count(sql) self.assertEqual(count,1) except AssertionError as e: self.excel.write_excel(row=row, column=8, value='未通过') mylog.info('用例:{}----->执行未通过'.format(case['title'])) mylog.error(e) print("预期结果:{}".format(expected)) print("实际结果:{}".format(res)) raise e else: self.excel.write_excel(row=row, column=8, value='已通过') mylog.info('用例:{}----->执行已通过'.format(case['title']))