def test_withdraw(self, item): url = conf.get('env', 'base_url') + item['url'] method = item['method'].lower() item['data'] = replace_data(item['data'], TestWithdraw) params = eval(item['data']) par_sign = HandleSign.generate_sign(self.token) params.update(par_sign) expected = eval(item['expected']) # --------请求接口前,查看用户余额-------- sql = "select leave_amount from futureloan.member where mobile_phone='{}'".format( conf.get('test_data', 'mobile')) start_wd = self.db.find_one(sql)[0] print("提现前用户余额为:{}".format(start_wd)) response = requests.request(method, url, json=params, headers=self.headers) res = response.json() # --------请求接口后,查看用户余额-------- end_wd = self.db.find_one(sql)[0] print("提现后用户余额为:{}".format(end_wd)) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if item['check_sql']: # 提现成功 self.assertEqual(float(start_wd - end_wd), params['amount']) else: self.assertEqual(float(start_wd - end_wd), 0) except AssertionError as e: log.error("用例--【{}】---执行失败".format(item['title'])) log.exception(e) raise e else: log.info("用例--【{}】---执行成功".format(item['title']))
def test_audit(self,item): url = conf.get('env','base_url')+ item['url'] item['data'] = replace_data(item['data'],TestAudit) params = eval(item['data']) par_sign = HandleSign.generate_sign(self.token) params.update(par_sign) method = item['method'].lower() expected = eval(item['expected']) response = requests.request(url=url,method=method,json=params,headers=self.admin_headers) res = response.json() if res['msg'] == 'OK' and item['title'] == '审核通过': TestAudit.pass_loan_id = params['loan_id'] try: 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: log.error("用例--【{}】---执行失败".format(item['title'])) log.exception(e) raise e else: log.info("用例--【{}】---执行成功".format(item['title']))
def test_addp(self, item): url = conf.get('env', 'base_url') + item['url'] method = item['method'] params = eval(replace_data(item['data'], Test_add)) par_sign = HandleSign.generate_sign(self.token) params.update(par_sign) expected = eval(item['expected']) # 调用接口前查询该用户项目数量 sql = "SELECT * FROM futureloan.loan WHERE member_id={}".format( self.member_id) strat_count = self.db.find_count(sql) print("调用接口之前的数量", strat_count) response = requests.request(url=url, method=method, json=params, headers=self.headers) res = response.json() end_count = self.db.find_count(sql) print("调用接口之后的数量", end_count) print("实际结果", res) print("预期结果", expected) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if res['msg'] == 'OK': self.assertEqual(end_count - strat_count, 1) except AssertionError as e: log.error("用例--【{}】---执行失败".format(item['title'])) log.exception(e) raise e else: log.info("用例--【{}】---执行成功".format(item['title']))
def test_invest(self, item): # 准备数据 url = conf.get('env', 'base_url') + item['url'] item['data'] = replace_data(item['data'], Test_invest) params = eval(item['data']) par_sign = HandleSign.generate_sign(self.token) params.update(par_sign) method = item['method'].lower() expected = eval(item['expected']) # ----------------投资前查询数据库--------------------------------- # 查用户表的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(url=url, method=method, 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.assertEqual(expected['msg'], res['msg']) # 断言实际结果中的msg是否包含 预期结果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: log.error("用例--【{}】---执行失败".format(item['title'])) log.exception(e) raise e else: log.info("用例--【{}】---执行成功".format(item['title']))
def test_register(self,item): url = conf.get('env','url')+ item['url'] method = item['method'] parms = eval(replace_data(item['data'])) expected = eval(replace_data(item['expected'])) res = requests.request(method=method,url=url,json=parms).json() try: self.ast.ass(expected,res) except AssertionError as e: log.info('用例--【{}】---执行失败'.format(item['title'])) raise e else: log.info('用例--【{}】---执行成功'.format(item['title']))
def test_register(self, itme): # 第一步:准备用例数据 # 1.接口地址 url = self.base_url + itme['url'] # 2.请求参数 if '#mobile#' in itme['data']: setattr(TestRegister, 'mobile', self.ran_phone()) itme['data'] = replace_data(itme['data'], TestRegister) params = eval(itme['data']) print(params) # 3.请求头 # 4.获取请求方法,转换为小写 method = itme['method'].lower() # 5.预期结果 expected = eval(itme['expected']) # --------请求接口前,查看是否注册改手机号-------- sql = "select * from futureloan.member where mobile_phone ='{}';".format( params['mobile_phone']) res1 = self.db.find_count(sql) print("{}请求接口前注册结果{}".format(params['mobile_phone'], res1)) # 第二步:请求接口,返回实际结果 response = requests.request(method, url, json=params, headers=self.headers) res = response.json() # --------请求接口后,查看是否注册改手机号-------- res2 = self.db.find_count(sql) print("{}请求接口前注册结果{}".format(params['mobile_phone'], res2)) # 第三步:断言 try: # 断言code和msg是否一致 self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if itme['check_sql']: # 注册成功,可以查到一条数据 self.assertEqual(res2, 1) except AssertionError as e: # 记录日志 log.error("用例--【{}】---执行失败".format(itme['title'])) log.exception(e) # 回写结果到excel(根据需求,回写需花费大量时间) raise e else: log.info("用例--【{}】---执行成功".format(itme['title']))
def test_login(self,itme): url = self.base_url+ itme['url'] method = itme['method'].lower() itme['data'] = replace_data(itme['data'],TestLogin) param = eval(itme['data']) expected = eval(itme['expected']) # 请求接口获取实际结果 response = requests.request(method,url,json=param,headers=self.headers) res = response.json() #断言 try: self.assertEqual(expected['code'],res['code']) self.assertEqual(expected['msg'],res['msg']) except AssertionError as e: log.error("用例--【{}】---执行失败".format(itme['title'])) log.exception(e) raise e else: log.info("用例--【{}】---执行成功".format(itme['title']))
def test_createcase(self, item): url = conf.get('env', 'url') + item['url'] method = item['method'] parms = eval(replace_data(item['data'])) headers = {"Authorization": getattr(CaseDate, "token")} res = requests.request(method=method, url=url, json=parms, headers=headers).json() while jsonpath.jsonpath(res, '$.id'): CaseDate.caseid = jsonpath.jsonpath(res, '$.id')[0] break expected = eval(replace_data(item['expected'])) try: self.ast.ass(expected, res) except AssertionError as e: log.info('用例--【{}】---执行失败'.format(item['title'])) raise e else: log.info('用例--【{}】---执行成功'.format(item['title']))
def test_recharge(self,item): # 第一步准备数据 url = conf.get('env','base_url') + item['url'] # 动态处理需要替换得参数 item['data'] = replace_data(item['data'],TestRecharge) params = eval(item['data']) par_sign = HandleSign.generate_sign(self.token) params.update(par_sign) expected = eval(item['expected']) method = item['method'].lower() # -------------请求之前获取数据库中用户余额-------------- sql = "select leave_amount from futureloan.member where mobile_phone='{}'".format(conf.get('test_data', 'mobile')) #执行查询sql start_amount = self.db.find_one(sql)[0] print("用例执行前余额:{}".format(start_amount)) # 第二步发送请求获取实际结果 response = requests.request(method,url,json=params,headers=self.headers) res = response.json() # -------------请求接口之后获取数据库中用户余额-------------- end_amount = self.db.find_one(sql)[0] print("用例执行后余额:{}".format(start_amount)) # assert try: self.assertEqual(expected['code'],res['code']) self.assertEqual(expected['msg'],res['msg']) # -------------校验数据库中用户余额的变化是否等于充值金额-------------- if item['check_sql']: # 充值成功,用户余额变化为充值金额 self.assertEqual(float(end_amount-start_amount),params['amount']) else: self.assertEqual(float(end_amount-start_amount),0) except AssertionError as e: log.error("用例--【{}】---执行失败".format(item['title'])) log.exception(e) raise e else: log.info("用例--【{}】---执行成功".format(item['title']))
# import unittest # from BeautifulReport import BeautifulReport # # suite=unittest.defaultTestLoader.discover(r'F:\python37test\02porttest20200829\day18') # br=BeautifulReport(suite) # br.report('demo2报告','report.html') import unittest import os from BeautifulReport import BeautifulReport from common.handle_path import TESTCASE_DIR, REPORT_DIR from common.handler_log import log from common.send_email import send_msg from HTMLTestRunnerNew import HTMLTestRunner log.info('-------测试开始执行--------') suite = unittest.TestSuite() loader = unittest.TestLoader() suite.addTest(loader.discover(TESTCASE_DIR)) # br=BeautifulReport(suite) # br.report('测试接口',filename='portreport.html',report_dir=REPORT_DIR) runner = HTMLTestRunner( stream=open(os.path.join(REPORT_DIR, 'report.html'), 'wb'), title='27期测试报告', tester='lisa', description='第一个版本的测试', ) runner.run(suite)