def setUp(self): self.mysql = MysqlTool() # 取现前用户可用余额 self.seek_leaveamount_sql = 'SELECT LeaveAmount FROM future.member where MobilePhone={0};'.format( Context.normal_phone) self.before_amount = self.mysql.fetch_one( self.seek_leaveamount_sql)['LeaveAmount'] # 取现前用户可用余额
def setUp(self): #获取期望结果放在setUp,充值前先取出leavemount sql = ReadConfig().get('SQL', 'LeaveAmount') global leavemount1 global mysql mysql = MysqlTool() leavemount1 = mysql.fetch_one(sql)['LeaveAmount'] mylogger.info('充值前账户余额:{0}'.format(leavemount1)) return leavemount1
def setUp(self): self.mysql = MysqlTool() # 投资前用户余额 self.seek_mount = ReadConfig().get('SQL', 'LeaveAmount') self.leaveamount1 = self.mysql.fetch_one( self.seek_mount)['LeaveAmount'] # 投资前余额 my_logger.info('投资前用户账户余额:{0}'.format(self.leaveamount1))
class TestAddLoan(unittest.TestCase): def setUp(self): self.mysql = MysqlTool() @data(*cases) def test_add(self, case): global Cookies my_logger.info('正在执行第{0}条用例{1}'.format(case.case_id, case.title)) url = ReadConfig().get('test_api', 'url_pre') + case.url my_logger.info('检查url接口地址:{0}'.format(url)) data = DoRegex().replace(case.data) data = json.loads(data) my_logger.info('检查请求参数{0}'.format(data)) my_logger.info('---------开始http请求-----------') resp = HttpRequest(method=case.method, url=url, data=data, cookies=Cookies) my_logger.info('---------结束http请求-----------') my_logger.info('请求接口结果是:{0}'.format(resp.get_json())) try: self.assertEqual(case.expected, int(resp.get_json()['code'])) Test_result = 'Pass' except AssertionError as e: Test_result = 'Failed' my_logger.error('出错了,{0}'.format(e)) raise e if resp.get_cookie(): Cookies = resp.get_cookie() my_logger.info('执行本条用例测试结果:{0}'.format(Test_result)) # 数据验证 # 当加标成功,根据借款人Id查询数据库load表是否有一条项目记录 if resp.get_json()['msg'] == '加标成功': expected = case.data seek_load_sql = 'select * from future.loan where MemberID={0} ORDER BY CreateTime DESC;'.format( Context.borrow_member_id) loan = self.mysql.fetch_one(seek_load_sql) if loan: # 查询标的记录非空 actual = { 'memberId': str(loan['MemberID']), 'title': loan['Title'], 'amount': int(loan['Amount']), 'loanRate': float(loan['LoanRate']), 'loanTerm': loan['LoanTerm'], 'loanDateType': loan['LoanDateType'], 'repaymemtWay': loan['RepaymemtWay'], 'biddingDays': loan['BiddingDays'] } self.assertDictEqual(expected, actual) my_logger.info('加标成功!查询load表新增一条项目记录') else: # 如果查询标的记录为空,测试不通过 raise AssertionError def tearDown(self): self.mysql.close()
class TestWithDraw(BaseCase): def setUp(self): self.mysql = MysqlTool() # 取现前用户可用余额 self.seek_leaveamount_sql = 'SELECT LeaveAmount FROM future.member where MobilePhone={0};'.format( Context.normal_phone) self.before_amount = self.mysql.fetch_one( self.seek_leaveamount_sql)['LeaveAmount'] # 取现前用户可用余额 @data(*cases) def test_withdraw(self, case): my_logger.info('正在执行第{0}条用例{1}'.format(case.case_id, case.title)) url = ReadConfig().get('test_api', 'url_pre') + case.url my_logger.info('检查url接口地址:{0}'.format(url)) if hasattr(Context, 'cookies'): cookies = getattr(Context, 'cookies') else: cookies = None data = DoRegex().replace(case.data) data = json.loads(data) my_logger.info('---------开始http请求-----------') resp = HttpRequest(method=case.method, url=url, data=data, cookies=cookies) my_logger.info('---------结束http请求-----------') my_logger.info('请求接口结果是:{0}'.format(resp.get_json())) try: self.assertEqual(case.expected, int(resp.get_json()['code'])) Test_result = 'Pass' except AssertionError as e: Test_result = 'Failed' my_logger.error('出错了{0}'.format(e)) raise e my_logger.info('本条用例执行结果:{0}'.format(Test_result)) # 数据验证 # 提现成功后,用户的可用余额减少 if resp.get_json()['msg'] == '取现成功': withdraw_amount = data['amount'] # 取现金额 actual = float( self.mysql.fetch_one( self.seek_leaveamount_sql)['LeaveAmount']) # 提现后金额 expected = float(self.before_amount) - float( withdraw_amount) # 提现前用户可用金额 - 提现金额 self.assertEqual(expected, actual) setattr(Context, 'after_withdraw_mount', actual) # 提现后金额放上下文中,为后面查流水记录准备 my_logger.info('提现成功后!查询数据库member表中用户减少余额和提现金额一致') # 验证新增一条流水记录 seek_financeLog_sql = 'SELECT * FROM future.financelog where PayMemberId={0} and Amount={1} ' \ 'and PayMemberMoney={2} ORDER BY CreateTime DESC;'.format(Context.memberId,data['amount'],getattr(Context,'after_withdraw_mount')) seek_financeLog = self.mysql.fetch_one(seek_financeLog_sql) my_logger.info(seek_financeLog) if seek_financeLog is not None: expected = { 'PayMemberId': int(Context.memberId), 'Amount': int(data['amount']), 'PayMemberMoney': float(getattr(Context, 'after_withdraw_mount')) } actual = { 'PayMemberId': seek_financeLog['PayMemberId'], 'Amount': int(seek_financeLog['Amount']), 'PayMemberMoney': float(seek_financeLog['PayMemberMoney']) } self.assertEqual(expected, actual) my_logger.info('查询financeLog表新增流水记录与提现请求参数一致') else: raise AssertionError else: # 提现失败,用户可用余额不变 actual = float( self.mysql.fetch_one( self.seek_leaveamount_sql)['LeaveAmount']) # 提现后可用余额 expected = float(self.before_amount) # 提现前可用余额 self.assertEqual(expected, actual) def tearDown(self): self.mysql.close()
def setUpClass(cls): global mysql mysql = MysqlTool()
class TestInvest(unittest.TestCase): def setUp(self): self.mysql = MysqlTool() # 投资前用户余额 self.seek_mount = ReadConfig().get('SQL', 'LeaveAmount') self.leaveamount1 = self.mysql.fetch_one( self.seek_mount)['LeaveAmount'] # 投资前余额 my_logger.info('投资前用户账户余额:{0}'.format(self.leaveamount1)) @data(*cases) def test_invest(self, case): my_logger.info('目前正在执行第{0}条用例:{1}'.format(case.case_id, case.title)) my_logger.info('----------开始检查请求url地址------------') global Cookies url = ReadConfig().get('test_api', 'url_pre') + case.url my_logger.info(url) my_logger.info('-------------开始检查请求数据-----------') global data data = DoRegex().replace(case.data) data = json.loads(data) my_logger.info(data) my_logger.info('----------开始http接口请求----------') resp = HttpRequest(method=case.method, url=url, data=data, cookies=Cookies) my_logger.info('----------结束http接口请求----------') my_logger.info('请求结果是{0}'.format(resp.get_json())) try: self.assertEqual(case.expected, int(resp.get_json()['code'])) Test_result = 'Pass' except AssertionError as e: my_logger.error('出错了,{0}'.format(e)) Test_result = 'Failed' raise e if resp.get_cookie(): # 如果cookies非空 Cookies = resp.get_cookie() my_logger.info('本条用例执行结果:{0}'.format(Test_result)) # 数据验证 # 当创建标的成功,根据借款人Id查询数据库load表是否添加一条记录 if resp.get_json()['msg'] == '加标成功': seek_load_sql = 'select * from future.loan where MemberID={0} ORDER BY CreateTime DESC;'.format( Context.borrow_member_id) loan = self.mysql.fetch_one(seek_load_sql) actual = { 'memberId': str(loan['MemberID']), 'title': loan['Title'], 'amount': int(loan['Amount']), 'loanRate': float(loan['LoanRate']), 'loanTerm': loan['LoanTerm'], 'loanDateType': loan['LoanDateType'], 'repaymemtWay': loan['RepaymemtWay'], 'biddingDays': loan['BiddingDays'] } if loan: self.assertDictEqual(data, actual) # 字典多个字段同时验证是否一致 my_logger.info('创建标的成功,查询数据库loan表与请求参数一致') setattr(Context, 'loanId', str(loan['Id'])) #将新建的标的id放入上下文中,为后面审核和投资用例正则替换准备 else: raise AssertionError # 如果查询数据库为空,测试不通过 # 当审核通过,验证数据库表中loan表Status字段更改 if resp.get_json()['msg'] == '更新状态成功:竞标开始,当前标为竞标中状态': seek_load_sql = 'select * from future.loan where MemberID={0} ORDER BY CreateTime DESC;'.format( ReadConfig().get('basic', 'borrow_member_id')) loan_status = self.mysql.fetch_one(seek_load_sql)['Status'] if loan_status: #查询非空 self.assertEqual(4, loan_status) my_logger.info('新增标的审核通过!数据库loan表Status字段更改为4正确') else: raise AssertionError # 投资成功,验证数据库是否生成投资记录、用户余额减少、新增一条流水记录 if resp.get_json()['msg'] == '竞标成功': invest_mount = data['amount'] # 投资金额 # 查投资后用户账户余额 actual = float( self.mysql.fetch_one( self.seek_mount)['LeaveAmount']) # 投资后用户余额 setattr(Context, 'after_invest_mount', actual) # 投资后金额放入上下文,为后面查流水记录准备 expected = float(self.leaveamount1) - float( invest_mount) # 投资前用户余额 - 投资金额 self.assertEqual(expected, actual) my_logger.info('投资成功!查询数据库member表用户账户减少余额和投资金额一致') # 查投资记录invest表是否有记录 seek_invest_sql = 'SELECT * FROM future.invest where MemberID={0} and LoanId ={1} and Amount={2} ' \ 'ORDER BY CreateTime DESC;'.format(ReadConfig().get('basic', 'memberId'), getattr(Context,'loanId'), data['amount']) invest_record = self.mysql.fetch_one(seek_invest_sql) if invest_record: # 如果数据库查询有记录 ,判断数据库中记录与请求参数是否一致 expected = { 'MemberID': int(data['memberId']), 'LoanId': int(data['loanId']), 'Amount': int(data['amount']) } actual = { 'MemberID': invest_record['MemberID'], 'LoanId': invest_record['LoanId'], 'Amount': invest_record['Amount'] } self.assertDictEqual(expected, actual) my_logger.info('invest表新增一条记录与请求参数一致') #查流水记录表financeLog是否有记录 seek_financeLog_sql = 'SELECT * FROM future.financelog where PayMemberId={0} and IncomeMemberId={1} and Amount={2} ' \ 'and PayMemberMoney={3} ORDER BY CreateTime DESC;'.format(Context.memberId,Context.borrow_member_id, data['amount'], getattr(Context,'after_invest_mount')) seek_financeLog = self.mysql.fetch_one(seek_financeLog_sql) if seek_financeLog: #如果查询流水记录表有数据 expected = { 'PayMemberId': int(data['memberId']), 'Amount': int(data['amount']), 'IncomeMemberId': int(Context.borrow_member_id), 'PayMemberMoney': float(getattr(Context, 'after_invest_mount')) } actual = { 'PayMemberId': seek_financeLog['PayMemberId'], 'Amount': int(seek_financeLog['Amount']), 'IncomeMemberId': seek_financeLog['IncomeMemberId'], 'PayMemberMoney': float(seek_financeLog['PayMemberMoney']) } self.assertDictEqual(expected, actual) my_logger.info('查询financeLog表新增流水记录与投资请求参数一致') else: # 如果查询流水记录表为空 raise AssertionError else: raise AssertionError else: # 投资失败 ,用户余额不变 actual = float( self.mysql.fetch_one( self.seek_mount)['LeaveAmount']) # 投资后用户余额 expected = float(self.leaveamount1) #投资前用户余额 self.assertEqual(expected, actual) def tearDown(self): self.mysql.close()
def setUp(self): self.mysql = MysqlTool()
def setUp(self): self.sql = ReadConfig().get('SQL', 'mobile_sql') self.mysql_tool = MysqlTool()
class TestRegister(unittest.TestCase): def setUp(self): self.sql = ReadConfig().get('SQL', 'mobile_sql') self.mysql_tool = MysqlTool() @data(*cases) def test_login(self, case): max_mobilephone = str( int(self.mysql_tool.fetch_one(self.sql)['MobilePhone']) + 1) # 查询数据库最大手机号+1 ReadConfig().write_value('MobilePhone', 'max_phone', max_mobilephone) # 最大手机号写入配置文件 my_logger.info('目前正在执行第{0}条用例:{1}'.format(case.case_id, case.title)) my_logger.info('----------开始检查url请求地址--------') url = ReadConfig().get('test_api', 'url_pre') + case.url my_logger.info('url接口地址是:{0}'.format(url)) #将excel读取出来的字符串转换成字典 my_logger.info('----------开始检查请求参数------------') data = case.data data = json.loads(SeekReplace().seek_replace(data)) my_logger.info('开始检查请求参数:{0}'.format(data)) resp = HttpRequest(method=case.method, url=url, data=data) try: self.assertEqual(case.expected, resp.get_text()) Test_result = 'Pass' except AssertionError as e: Test_result = 'Failed' print('断言出错了%s:', e) raise e my_logger.info('本条用例的测试结果:{}'.format(Test_result)) # 数据验证 if int(resp.get_json()['code']) == 10001: # 注册成功,数据库查询有记录 sql = 'SELECT * FROM future.member where MobilePhone="{0}"'.format( max_mobilephone) expected = max_mobilephone resp = self.mysql_tool.fetch_one(sql) if resp is not None: # 判断查找数据库数据非空 self.assertEqual(expected, resp['MobilePhone']) my_logger.info('注册成功,数据库查询正确') else: # 如果查询为空, my_logger.info('注册成功,数据库查询无数据') raise AssertionError else: # 注册不成功,数据库查询为空 sql = 'SELECT * FROM future.member where MobilePhone="{0}"'.format( max_mobilephone) member = self.mysql_tool.fetch_one(sql) if member is None: # 如果查询数据库为空 expected = None self.assertEqual(expected, member) my_logger.info('注册不成功,数据库查询无增加数据') else: # 查询数据库非空 my_logger.info('注册不成功,数据库查询增加数据') raise AssertionError def tearDown(self): self.mysql_tool.close()