def test_login(self, case): # pass method = case['method'] row = case['case_id'] + 1 url = 'http://api.lemonban.com/futureloan' + case['url'] data = eval(case['data']) expected = eval(case['expected']) headers = eval(conf.get('env', 'headers')) response = request(method=method, json=data, headers=headers, url=url) res = response.json() # print('预期结果',expected) # print('实际结果',res) try: self.assertEqual(expected['msg'], res['msg']) self.assertEqual(expected['code'], res['code']) except AssertionError as e: self.excel.write_data(row=row, column=8, value='fail') log.error('用例--{}--执行未通过'.format(case['title'])) log.exception(e) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:{}'.format(res)) raise e else: self.excel.write_data(row=row, column=8, value='pass') log.info('用例--{}--执行通过'.format(case['title']))
def test_loans(self, case): expected = eval(case['expected']) url = conf.get('env', 'BASE_URL') + case['url'] method = case['method'] data = eval(case['data']) headers = eval(conf.get('env', 'headers')) # headers['Authorization'] = getattr(EnvData, 'token') row = case['case_id'] + 1 response = request(method=method, url=url, headers=headers, params=data) res = response.json() print('实际结果:', res) print('预期结果:', expected) try: self.assertEqual(res['code'], expected['code']) self.assertEqual(res['msg'], expected['msg']) # 校验返回数据条数是否和预期一致 self.assertEqual(len(res['data']), expected['len']) except AssertionError as e: log.error('__测试案例{}__测试不通过_'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') log.debug('实际结果:', res) log.debug('预期结果:', expected) raise e else: self.excel.write_data(row=row, column=8, value='pass')
def test_main(self, case): method = case['method'] if '#member_id#' in case['url']: case['url'] = replace_data(case['url']) url = 'http://api.lemonban.com/futureloan' + case['url'] row = case['case_id'] + 1 # 替换手机号码 if '#mobile_phone#' in case['data']: if case['interface'] == 'register': mobile_phone = self.random_phone() setattr(EnvData, 'mobile_phone', mobile_phone) # case['data'] = case['data'].replace('#mobile_phone#', mobile_phone) case['data'] = replace_data(case['data']) print(mobile_phone) else: # mobile_phone=getattr(EnvData,'mobile_phone') case['data'] = replace_data(case['data']) data = eval(replace_data(case['data'])) print(data) expected = eval(case['expected']) headers = eval(conf.get('env', 'headers')) if case['interface'] != 'register' and case['interface'] != 'login': headers['Authorization'] = getattr(EnvData, 'token') # elif case['interface'] != 'login': # headers['Authorization'] = getattr(EnvData, 'token') response = request(method=method, url=url, json=data, headers=headers) res = response.json() print('预期结果:', expected) print('实际结果:', res) if case['interface'] == 'login': member_id = str(jsonpath(res, '$..id')[0]) token = 'Bearer ' + jsonpath(res, '$..token')[0] setattr(EnvData, 'member_id', member_id) setattr(EnvData, 'token', token) if case['interface'] == 'add': loan_id = str(jsonpath(res, '$..id')[0]) setattr(EnvData, 'loan_id', loan_id) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) except AssertionError as e: log.debug('预期结果:', expected) log.debug('实际结果:', res) log.error('测试用例--{}--测试不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') raise e else: self.excel.write_data(row=row, column=8, value='pass') log.error('测试用例--{}--测试通过'.format(case['title']))
def test_recharge(self,case): # 调用新的继承的类 row=case['case_id']+1 url=conf.get('env','BASE_URL')+case['url'] method=case['method'] expected=eval(case['expected']) # 准备用例数据 # 替换用户Id # if '#member_id#' in case['data']: # case['data']=case['data'].replace('#member_id#',self.member_id) # data = eval(case['data']) data = eval(replace_data(case['data'])) # 准备请求头 headers=eval(conf.get('env','headers')) headers['Authorization']=getattr(EnvData,'token') # 判断该用例是否需要数据库校验 if case['check_sql']: sql=case['check_sql'].format(getattr(EnvData,'member_id')) start_money=self.db.find_one(sql)['leave_amount'] print('充值之前的金额',start_money) response=request(url=url, method=method, headers=headers, json=data) res=response.json() print('预期结果',expected) print('实际结果',res) # 获取充值之后的账户余额 if case['check_sql']: sql=case['check_sql'].format(getattr(EnvData,'member_id')) end_money=self.db.find_one(sql)['leave_amount'] print('充值之后的金额',end_money) try: self.assertEqual(res['code'],expected['code']) self.assertEqual(res['msg'],expected['msg']) # 判断是否需要进行sql校验 if case['check_sql']: self.assertEqual(end_money-start_money,Decimal(str(data['amount']))) except AssertionError as e: self.excel.write_data(row=row, column=8, value='fail') log.error('用例--{}--执行未通过'.format(case['title'])) log.exception(e) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:{}'.format(res)) raise e else: self.excel.write_data(row=row, column=8, value='pass') log.info('用例--{}--执行通过'.format(case['title']))
def test_audit(self, case): url = conf.get('env', 'BASE_URL') + case['url'] expected = eval(case['expected']) # 判断是否需要替换审核通过的id # if '#pass_loan_id#' in case['data']: # # 将之前保存的审核通过的标Id替换到测试用例中 # case['data']=replace_data(case['data']) # print(case['data']) # data = eval(case['data'].replace('#loan_id#',str(self.loan_id))) # case['data']=replace_data(case['data']) # if '#loan_id#' in case['data']: data = eval(replace_data(case['data'])) method = case['method'] row = case['case_id'] + 1 headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'admin_token') if case['title'] == '普通用户审核': headers['Authorization'] = getattr(EnvData, 'token') response4 = request(url=url, json=data, headers=headers, method=method) res4 = response4.json() print('实际结果', res4) # 判断是否是审核通过的案例并且审核成功 if case['title'] == '审核通过' and expected['msg'] == 'OK': # 将执行通过的标ID保存为类属性 pass_loan_id = str(data['loan_id']) setattr(EnvData, 'pass_loan_id', pass_loan_id) try: self.assertEqual(res4['code'], expected['code']) self.assertEqual(res4['msg'], expected['msg']) if case['check_sql']: sql = replace_data(case['check_sql']) # sql = case['check_sql'].replace('#loan_id#', str(self.loan_id)) end_status = self.db.find_one(sql)['status'] print('加标后的状态:', end_status) self.assertEquals(expected['status'], end_status) except AssertionError as e: log.debug('预期结果:', expected) log.debug('实际结果:', res4) log.error('测试用例--{}--测试不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') raise e else: self.excel.write_data(row=row, column=8, value='pass') log.error('测试用例--{}--测试通过'.format(case['title']))
def test_add(self, case): headers = eval(conf.get('env', 'headers')) expected = eval(case['expected']) headers['Authorization'] = getattr(EnvData, 'token') # if '#member_id#' in case['data']: # case['data']=case['data'].replace('#member_id#',self.member_id) # 替换用例中的动态数据 # case['data']=replace_data(case['data']) # data=eval(case['data']) # case['data']= data = eval(replace_data(case['data'])) url = conf.get('env', 'BASE_URL') + case['url'] method = case['method'] row = case['case_id'] + 1 if case["check_sql"]: # sql= case['check_sql'].replace('#member_id#', self.member_id) sql = replace_data(case["check_sql"]) start_count = self.db.count(sql) print('加标前的数量:', start_count) response = request(url=url, json=data, headers=headers, method=method) res = response.json() print('预期结果:', expected) print('实际结果:', res) try: self.assertEqual(res['code'], expected['code']) self.assertEqual(res['msg'], expected['msg']) if case['check_sql']: # sql = case['check_sql'].replace('#member_id#', self.member_id) sql = replace_data(case['check_sql']) end_count = self.db.count(sql) print('加标后的数量:', end_count) self.assertEquals(1, end_count - start_count) except AssertionError as e: log.debug('预期结果:', expected) log.debug('实际结果:', res) log.error('测试用例--{}--测试不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') raise e else: self.excel.write_data(row=row, column=8, value='pass') log.error('测试用例--{}--测试通过'.format(case['title']))
def test_register(self, case): # pass method = case['method'] url = 'http://api.lemonban.com/futureloan' + case['url'] row = case['case_id'] + 1 # 替换手机号码 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')) response = requests.request(method=method, url=url, headers=headers, json=data) res = response.json() # print('结果是',res) # print('预期结果', expected) # print('实际结果', res) try: self.assertEqual(res['code'], expected['code']) self.assertEqual(res['msg'], expected['msg']) # 判断是否需要进行sql校验 if case['check_sql']: sql = case['check_sql'].replace('#phone#', data['mobile_phone']) # res=self.db.find_one(sql) # self.db.close() # 判断是否有数据 # self.assertTrue(res) res2 = self.db.count(sql) self.assertEqual(1, res2) # self.db.close() except AssertionError as e: self.excel.write_data(row=row, column=8, value='fail') log.error('用例--{}--执行未通过'.format(case['title'])) log.exception(e) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:{}'.format(res)) raise e else: self.excel.write_data(row=row, column=8, value='pass') log.info('用例--{}--执行通过'.format(case['title']))
def test_withdraw(self, case): # 测试数据需要去除参数化 # if '#member_id#' in case['data']: # case['data']=case['data'].replace('#member_id#',) data = eval(replace_data(case['data'])) expected = eval(case['expected']) method = case['method'] url = conf.get('env', 'BASE_URL') + case['url'] # 请求头要eval headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') row = case['case_id'] + 1 if case['check_sql']: sql = replace_data(case['check_sql']) start_money = self.db.find_one(sql)['leave_amount'] print('提现之前的余额:', start_money) response = request(method=method, url=url, headers=headers, json=data) res = response.json() print('预期结果:', expected) print('实际结果:', res) if case['check_sql']: sql = replace_data(case['check_sql']) end_money = self.db.find_one(sql)['leave_amount'] print('提现之后的余额:', end_money) try: self.assertEqual(res['code'], expected['code']) self.assertEqual(res['msg'], expected['msg']) if case['check_sql']: # Decimal需要使用str修饰 # 金额不是在case里面,是在data里面的amount self.assertEqual(start_money - end_money, Decimal(str(data['amount']))) except AssertionError as e: log.error('测试———{}—————执行未通过'.format(case['title'])) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:', res) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') raise e else: self.excel.write_data(row=row, column=8, value='pass') log.info('测试———{}—————执行通过'.format(case['title']))
def test_invest(self, case): '''投资用例''' headers = eval(conf.get('env', 'headers')) # 如果不是登陆请求,则添加一个token if case['interface'] != 'login': headers['Authorization'] = getattr(EnvData, 'token') url = conf.get('env', 'BASE_URL') + case['url'] method = case['method'] # case['data']=replace_data(case['data']) # print(case['data']) data = eval(replace_data(case['data'])) expected = eval(case['expected']) row = case['case_id'] + 1 if case['check_sql']: sql1 = 'select * from futureloan.invest where member_id= #member_id#;' start_invest_count = self.db.count(replace_data(sql1)) sql2 = 'select leave_amount from futureloan.member where id= #member_id#;' start_leave_amount = self.db.find_one( replace_data(sql2))['leave_amount'] sql3 = 'select * from futureloan.financelog where pay_member_id= #member_id#;' start_financelog_count = self.db.count(replace_data(sql3)) response = request(method=method, json=data, url=url, headers=headers) res = response.json() # 如果是登陆接口,则提取用户Id+token if case['interface'] == 'login': member_id = str(jsonpath(res, '$..id')[0]) token = 'Bearer ' + jsonpath(res, '$..token')[0] setattr(EnvData, 'member_id', member_id) setattr(EnvData, 'token', token) # 如果是加标接口,则提取标Id if case['interface'] == 'add': loan_id = str(jsonpath(res, '$..id')[0]) setattr(EnvData, 'loan_id', loan_id) print(getattr(EnvData, 'loan_id')) if case['check_sql']: sql1 = 'select * from futureloan.invest where member_id= #member_id#;' end_invest_count = self.db.count(replace_data(sql1)) sql2 = 'select leave_amount from futureloan.member where id= #member_id#;' end_leave_amount = self.db.find_one( replace_data(sql2))['leave_amount'] sql3 = 'select * from futureloan.financelog where pay_member_id= #member_id#;' end_financelog_count = self.db.count(replace_data(sql3)) try: self.assertEqual(res['code'], expected['code']) self.assertEqual(res['msg'], expected['msg']) # 判断是否需要进行sql校验 if case['check_sql']: self.assertEquals(1, end_invest_count - start_invest_count) self.assertEquals(Decimal(str(data['amount'])), start_leave_amount - end_leave_amount) self.assertEquals( 1, end_financelog_count - start_financelog_count) if case['title'] == '满标': sql4 = 'select id from futureloan.invest where loan_id =#loan_id#;' invest_ids = self.db.find_all(replace_data(sql4)) # setattr(EnvData,'invest_ids',invest_ids) for invest_id in invest_ids: sql5 = 'select * from futureloan.repayment where invest_id={}'.format( invest_id['id']) count = self.db.count(sql5) # 只要数据条数不为0即可校验通过 self.assertTrue(count) except AssertionError as e: self.excel.write_data(row=row, column=8, value='fail') log.error('用例--{}--执行未通过'.format(case['title'])) log.exception(e) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:{}'.format(res)) raise e else: self.excel.write_data(row=row, column=8, value='pass') log.info('用例--{}--执行通过'.format(case['title']))