def test_register(self, case): '''注册用例类''' global result #判断需要动态生成手机号码的用例数据,进行替换 if '#phoneNumber#' in case['data']: phone = randomPhoneNumb() case['data'] = case['data'].replace('#phoneNumber#', phone) mylog.info("正在测试第{}个用例,测试数据是:{}".format(case['case_id'], case)) data = json.loads(case['data']) headers = json.loads(case['headers']) #预期结果 mylog.info('用例期望结果是:{}'.format(json.loads(case['expected']))) #实际结果 try: res = doRequests(url=MiddleHandler().host + case['url'], method=case['method'], json=data, headers=headers).json() except AssertionError as e: mylog.error('错误信息:数据返回不是json,{}'.format(e)) raise e mylog.info('用例实际结果是:{}'.format(res)) #检查点 expected_dict = json.loads(case['expected']) try: for k, v in expected_dict.items(): self.assertEqual(v, res[k]) # 判断是否需要进行SQL校验 if res['code'] == 0: # 数据库操作 db = MiddleHandler.getDb() sqldata = db.query( "select * from member where mobile_phone={}".format( data['mobile_phone'])) db.close() mylog.info('插入的数据为{}'.format(data)) try: #是否存在注册数据 self.assertTrue(sqldata) #注册的手机号码是否与数据库一致 self.assertEqual(sqldata['mobile_phone'], data['mobile_phone']) result = '通过' except AssertionError as error: result = '不通过' raise error #不需要校验SQL else: result = '通过' #捕获异常 except AssertionError as e: mylog.error('错误信息{}'.format(e)) result = '不通过' raise e finally: mylog.info('用例{}:测试{},正在回写测试结果...'.format(case['case_id'], result)) MiddleHandler.execl.write(MiddleHandler.file_name, 'register', case['case_id'] + 1, 9, result) mylog.info('回写测试结果成功!')
def test_audit(self, case): global result case['data'] = handler.replace_data(case['data']) case['headers'] = handler.replace_data(case['headers']) data = eval(case['data']) headers = json.loads(case['headers']) expected = json.loads(case['expected']) mylog.info("正在测试第{}个用例,测试数据是:{}".format(case['case_id'], case)) mylog.info('用例期望结果是:{}'.format(expected)) #替换headers res = doRequests(url=MiddleHandler().host + case['url'], method=case['method'], headers=headers, json=data).json() mylog.info('用例实际结果是:{}'.format(res)) try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if res['code'] == 0: loan_data = self.db.query( 'select * from loan where id={}'.format(self.loan_id)) self.assertEqual(expected['status'], loan_data['status']) result = '通过' except AssertionError as e: mylog.error('错误信息{}'.format(e)) result = '不通过' raise e finally: mylog.info('用例{}:测试{},正在回写测试结果...'.format(case['case_id'], result)) MiddleHandler.execl.write(MiddleHandler.file_name, 'audit', case['case_id'] + 1, 9, result) mylog.info('回写测试结果成功!')
def setUpClass(cls) -> None: #普通用户 setattr(CaseVar, 'token', handler.login('user')['token']) setattr(CaseVar, 'member_id', handler.login('user')['member_id']) #管理员用户登录 setattr(CaseVar, 'admin_token', handler.login('admin_user')['token']) #投资人登录 setattr(CaseVar, 'invest_member_id', handler.login('invest_user')['member_id']) setattr(CaseVar, 'invest_token', handler.login('invest_user')['token']) #初始化数据库对象 cls.db = MiddleHandler.getDb() #审核中 audit_loan_id = cls.db.query( "select id from loan where member_id='12103' and status=1 order by id desc LIMIT 1;" ) setattr(CaseVar, 'audit_loan_id', audit_loan_id['id']) #还款中 repay_loan_id = cls.db.query( "select id from loan where member_id='12103' and status=3 order by id desc LIMIT 1;" ) setattr(CaseVar, 'repay_loan_id', repay_loan_id['id']) ##还款完成 repayall_loan_id = cls.db.query( "select id from loan where member_id='12103' and status=4 order by id desc LIMIT 1;" ) setattr(CaseVar, 'repayall_loan_id', repayall_loan_id['id'])
def test_withdraw(self, case): '''提现用例类''' global result #替换测试数据 member_id = getattr(CaseVar, 'member_id', "") token = getattr(CaseVar, 'token', "") # 替换测试数据 case['data'] = handler.replace_data(case['data']) case['headers'] = handler.replace_data(case['headers']) data = json.loads(case['data']) headers = json.loads(case['headers']) excepted = json.loads(case['expected']) #提现前余额与提现流水记录 before_amount = self.db.query( "select leave_amount from member where id={}".format(member_id)) before_count = self.db.query( "select count(1) as cn from financelog where pay_member_id={};". format(member_id)) mylog.info("正在测试第{}个用例,测试数据是:{}".format(case['case_id'], case)) mylog.info('用例期望结果是:{}'.format(excepted)) #发送请求 res = doRequests(url=MiddleHandler().host + case['url'], method=case['method'], headers=headers, json=data).json() mylog.info('用例实际结果是:{}'.format(res)) #检查点 try: for k, v in excepted.items(): self.assertEqual(v, res[k]) after_amount = self.db.query( "select leave_amount from member where id={}".format( member_id)) after_count = self.db.query( "select count(1) as cn from financelog where pay_member_id={};" .format(member_id)) if res['code'] == 0: #提现成功后,判断余额是否正确并存在流水记录 self.assertEqual( before_amount['leave_amount'] - Decimal(str(data['amount'])), after_amount['leave_amount']) self.assertEqual(str(before_count['cn'] + 1), str(after_count['cn'])) result = '通过' else: #提现异常后,判断余额是否正确 self.assertEqual(before_amount['leave_amount'], after_amount['leave_amount']) result = '通过' except AssertionError as e: mylog.error('异常信息{}'.format(e)) result = '不通过' raise e finally: mylog.info('用例{}:测试{},正在回写测试结果...'.format(case['case_id'], result)) MiddleHandler.execl.write(MiddleHandler.file_name, 'withdraw', case['case_id'] + 1, 9, result) mylog.info('回写测试结果成功!')
def test_recharge(self, case): '''充值用例类''' global result member_id = getattr(CaseVar, 'member_id', "") #替换测试数据 case['data'] = handler.replace_data(case['data']) case['headers'] = handler.replace_data(case['headers']) data = json.loads(case['data']) headers = json.loads(case['headers']) excepted = json.loads(case['expected']) before_money = self.db.query( "select leave_amount from member where id={}".format(member_id)) mylog.info("正在测试第{}个用例,测试数据是:{}".format(case['case_id'], case)) mylog.info('用例期望结果是:{}'.format(excepted)) #发送请求 res = doRequests(url=MiddleHandler().host + case['url'], method=case['method'], headers=headers, json=data) res = res.json() mylog.info('用例实际结果是:{}'.format(res)) #检查点 try: for k, v in excepted.items(): self.assertEqual(v, res[k]) after_amount = self.db.query( "select leave_amount from member where id={}".format( member_id)) if res['code'] == 0: #判断余额是否正确 self.assertEqual( before_money['leave_amount'] + Decimal(str(data['amount'])), after_amount['leave_amount']) financeLog = self.db.query( "select 1 from financelog where income_member_id='12103' and amount={}" .format(Decimal(str(data['amount'])))) self.assertIsNotNone(financeLog) result = '通过' else: #充异常后,判断余额是否不变 self.assertEqual(before_money['leave_amount'], after_amount['leave_amount']) result = '通过' except AssertionError as e: mylog.error('错误信息{}'.format(e)) result = '不通过' raise e finally: mylog.info('用例{}:测试{},正在回写测试结果...'.format(case['case_id'], result)) MiddleHandler.execl.write(file_path=MiddleHandler.file_name, sheet_name='recharge', row=case['case_id'] + 1, col=9, data=result) mylog.info('回写测试结果成功!')
def setUpClass(cls) -> None: #普通用户登录 setattr(CaseVar, 'member_id', handler.login('user')['member_id']) setattr(CaseVar, 'token', handler.login('user')['token']) #管理员用户登录 setattr(CaseVar, 'admin_token', handler.login('admin_user')['token']) cls.db = MiddleHandler.getDb() pass_loan = cls.db.query( "select id from loan where member_id='12103' and status=2 order by id desc LIMIT 1;" ) setattr(CaseVar, 'pass_loan_id', pass_loan['id'])
def randomPhoneNumb(): '''动态生成手机号码''' while True: phone = '138' for i in range(8): num = random.randint(0, 9) phone += str(num) # 数据库操作 db = MiddleHandler.getDb() phone_data = db.query( "select * from member where mobile_phone='{}'".format(phone)) if not phone_data: db.close() return phone db.close()
def test_add(self,case): global result member_id = getattr(CaseVar, 'member_id', "") #替换测试数据 case['data'] = handler.replace_data(case['data']) case['headers'] = handler.replace_data(case['headers']) data = eval(case['data']) excepted = json.loads(case['expected']) before_count=self.db.query("select count(1) as cn from loan where member_id={}".format(member_id)) mylog.info("正在测试第{}个用例,测试数据是:{}".format(case['case_id'], case)) mylog.info('用例期望结果是:{}'.format(excepted)) #发送请求 res=doRequests( url=MiddleHandler().host+case['url'], method=case['method'], headers=json.loads(case['headers']), json=data ) res=res.json() mylog.info('用例实际结果是:{}'.format(res)) #检查点 try: for k, v in excepted.items(): self.assertEqual(v, res[k]) after_count = self.db.query("select count(1) as cn from loan where member_id={}".format(member_id)) if res['code']==0: #判断是否新增1条项目记录 self.assertEqual(str(before_count['cn']+1),str(after_count['cn'])) result = '通过' except AssertionError as e: mylog.error('错误信息{}'.format(e)) result = '不通过' raise e finally: mylog.info('用例{}:测试{},正在回写测试结果...'.format(case['case_id'], result)) MiddleHandler.execl.write(MiddleHandler.file_name, 'add', case['case_id'] + 1, 9, result) mylog.info('回写测试结果成功!')
import ddt import json import unittest from common.requestsHandler import doRequests from middleware.middleHandler import MiddleHandler, CaseVar #对象初始化 handler = MiddleHandler() #读取测试数据 cases = MiddleHandler().execl.read_data('audit') #初始化日志对象 mylog = MiddleHandler.mylog @ddt.ddt class TestAudit(unittest.TestCase): '''审核用例类''' @classmethod def setUpClass(cls) -> None: #普通用户登录 setattr(CaseVar, 'member_id', handler.login('user')['member_id']) setattr(CaseVar, 'token', handler.login('user')['token']) #管理员用户登录 setattr(CaseVar, 'admin_token', handler.login('admin_user')['token']) cls.db = MiddleHandler.getDb() pass_loan = cls.db.query( "select id from loan where member_id='12103' and status=2 order by id desc LIMIT 1;" ) setattr(CaseVar, 'pass_loan_id', pass_loan['id']) def setUp(self) -> None:
def setUpClass(cls) -> None: setattr(CaseVar, 'member_id', handler.login('user')['member_id']) setattr(CaseVar, 'token', handler.login('user')['token']) cls.db = MiddleHandler.getDb()
import ddt import json import unittest from decimal import Decimal from common.requestsHandler import doRequests from middleware.middleHandler import MiddleHandler from middleware.middleHandler import CaseVar #对象初始化 handler = MiddleHandler() #读取测试数据 cases = MiddleHandler().execl.read_data('recharge') #初始化日志对象 mylog = MiddleHandler.mylog @ddt.ddt class TestRecharge(unittest.TestCase): '''充值用例类''' @classmethod def setUpClass(cls) -> None: setattr(CaseVar, 'member_id', handler.login('user')['member_id']) setattr(CaseVar, 'token', handler.login('user')['token']) cls.db = MiddleHandler.getDb() def setUp(self) -> None: pass @classmethod def tearDownClass(cls) -> None: cls.db.close()
import ddt import json import unittest from common.requestsHandler import doRequests from middleware.middleHandler import MiddleHandler, CaseVar #对象初始化 handler = MiddleHandler() #读取测试数据 cases = handler.execl.read_data('invest') #初始化日志对象 mylog = MiddleHandler.mylog @ddt.ddt class TestInvest(unittest.TestCase): '''投资用例类''' @classmethod def setUpClass(cls) -> None: #普通用户 setattr(CaseVar, 'token', handler.login('user')['token']) setattr(CaseVar, 'member_id', handler.login('user')['member_id']) #管理员用户登录 setattr(CaseVar, 'admin_token', handler.login('admin_user')['token']) #投资人登录 setattr(CaseVar, 'invest_member_id', handler.login('invest_user')['member_id']) setattr(CaseVar, 'invest_token', handler.login('invest_user')['token']) #初始化数据库对象 cls.db = MiddleHandler.getDb() #审核中
def setUpClass(cls) -> None: setattr(CaseVar, 'member_id', handler.login('user')['member_id']) setattr(CaseVar, 'token', handler.login('user')['token']) cls.db = MiddleHandler.getDb() #根据用例数据设计,数据初始化 cls.db.query("update member set leave_amount='500100' where id=12103;")
import ddt import json import unittest from decimal import Decimal from common.requestsHandler import doRequests from middleware.middleHandler import MiddleHandler, CaseVar #对象初始化 handler = MiddleHandler() #读取测试数据 cases = MiddleHandler().execl.read_data('withdraw') #初始化日志对象 mylog = MiddleHandler.mylog @ddt.ddt class TestWithdraw(unittest.TestCase): '''提现用例类''' @classmethod def setUpClass(cls) -> None: setattr(CaseVar, 'member_id', handler.login('user')['member_id']) setattr(CaseVar, 'token', handler.login('user')['token']) cls.db = MiddleHandler.getDb() #根据用例数据设计,数据初始化 cls.db.query("update member set leave_amount='500100' where id=12103;") @classmethod def tearDownClass(cls) -> None: cls.db.close() @ddt.data(*cases)