def audit(loan_id, audit_status): """管理员测试账户审核一个标""" method = 'patch' url = Handler.yaml_conf['project']['host'] + '/loan/audit' administrator_token = Handler().administrator_token headers = { 'X-Lemonban-Media-Type': 'lemonban.v2', 'Authorization': administrator_token } data = {'loan_id': loan_id, 'approved_or_not': audit_status} visit_api(method, url, headers, json=data) return None
def recharge_borrower(amount): """借款测试账户充值""" method = 'post' url = Handler.yaml_conf['project']['host'] + '/member/recharge' borrower_token = Handler().borrower_token headers = { 'X-Lemonban-Media-Type': 'lemonban.v2', 'Authorization': borrower_token } borrower_id = Handler.yaml_conf['project']['borrower_id'] data = {'member_id': borrower_id, 'amount': amount} visit_api(method, url, headers, json=data) return None
def borrower_token(self): method = 'post' url = Handler.yaml_conf['project']['host'] + '/member/login' headers = {'X-Lemonban-Media-Type': 'lemonban.v2'} user = Handler.yaml_conf['borrower'] resp = visit_api(method, url, headers, json=user) token_type = jsonpath(resp, '$..token_type')[0] token_str = jsonpath(resp, '$..token')[0] return ' '.join([token_type, token_str])
def add(): """为借款测试账户加一个标""" method = 'post' url = Handler.yaml_conf['project']['host'] + '/loan/add' borrower_token = Handler().borrower_token headers = { 'X-Lemonban-Media-Type': 'lemonban.v2', 'Authorization': borrower_token } borrower_id = Handler.yaml_conf['project']['borrower_id'] data = { 'member_id': borrower_id, 'title': '开烧烤店', 'amount': 15000, 'loan_rate': 12, 'loan_term': 3, 'loan_date_type': 1, 'bidding_days': 5 } visit_api(method, url, headers, json=data) return None
def test_register(self, case_info): method = case_info['method'] url = host + case_info['url'] headers = eval(case_info['headers']) data = Handler().replace_data(case_info['data']) data = eval(data) expected_resp = eval(case_info['expected_resp']) actual_resp = visit_api(method, url, headers, json=data) try: for k, v in expected_resp.items(): self.assertEqual(actual_resp[k], v) if actual_resp['code'] == 0: # 使用封装默认的 fetchall 查询,查询到则结果为列表套字典,查询不到则结果为空元组 query_normal = MysqlHandlerMid().query( 'select * from futureloan.member where mobile_phone = "{}";' .format(data['mobile_phone']), False) self.assertTrue( query_normal) # 此条断言通过表示:查询到了手机号为 XXX 的记录,但是否仅有一条不能确定 self.assertTrue( len(query_normal) == 1) # 通过列表长度为 1,确认手机号为 XXX 的记录仅有一条 self.assertTrue(query_normal[0]['leave_amount'] == 0) self.assertTrue( int(str(datetime.now()).split(':')[1]) - int(str(query_normal[0]['reg_time']).split(':')[1]) <= 1) # 注册时间验证 if '*' in case_info['title']: # 用例 3 额外标记,进行不同断言 self.assertTrue( query_normal[0]['type'] == 1) # 不传 type,是否为普通会员 self.assertTrue(query_normal[0]['reg_name'] == '小柠檬') # 不传 reg_name,是否注册名为小柠檬 else: self.assertEqual(query_normal[0]['type'], data['type']) # 用例 1、2 self.assertEqual(query_normal[0]['reg_name'], data['reg_name']) # 用例 1、2 else: # 异常用例 if '$' not in case_info[ 'title']: # 除重复注册、手机号为空外的异常用例,需查库验证会不会异常插入到数据库 query_exceptional = MysqlHandlerMid().query( 'SELECT * from futureloan.member WHERE mobile_phone = "{}";' .format(data['mobile_phone']), False) self.assertFalse(query_exceptional) excel.write_data('register', case_info['case_id'] + 1, 8, '通过') logger.info('第 {} 条用例通过'.format(case_info['case_id'])) # 添加 KeyError 异常处理:当接口返回缺少应有字段,也代表用例不能通过 except (AssertionError, KeyError) as e: excel.write_data('register', case_info['case_id'] + 1, 8, '不通过') logger.error('第 {} 条用例不通过'.format(case_info['case_id'])) raise e
def test_add(self, case_info): method = case_info['method'] url = host + case_info['url'] headers = Handler().replace_data(case_info['headers']) headers = eval(headers) data = Handler().replace_data(case_info['data']) data = eval(data) expected_resp = eval(case_info['expected_resp']) actual_resp = visit_api(method, url, headers, json=data) bid_after = MysqlHandlerMid().query(self.sql)['count(*)'] try: for k, v in expected_resp.items(): self.assertEqual(actual_resp[k], v) if actual_resp['code'] == 0: self.assertTrue(bid_after == self.bid_before + 1) bid_info = MysqlHandlerMid().query( 'select * from futureloan.loan where member_id = {} order by create_time desc;' .format(Handler.borrower_id)) self.assertTrue(bid_info['member_id'], Handler.borrower_id) self.assertEqual(bid_info['title'], data['title']) self.assertEqual(bid_info['amount'], Decimal(str(data['amount']))) self.assertEqual(bid_info['loan_rate'], Decimal(str(data['loan_rate']))) self.assertEqual(bid_info['loan_term'], data['loan_term']) self.assertEqual(bid_info['loan_date_type'], data['loan_date_type']) # self.assertEqual(bid_info['bidding_days'], data['bidding_days']) self.assertTrue( int(str(datetime.now()).split(':')[1]) - int(str(bid_info['create_time']).split(':')[1]) <= 1) self.assertFalse(bid_info['bidding_start_time']) self.assertFalse(bid_info['full_time']) self.assertTrue(bid_info['status'] == 1) else: self.assertEqual(self.bid_before, bid_after) excel.write_data('add', case_info['case_id'] + 1, 8, '通过') logger.info('第 {} 条用例通过'.format(case_info['case_id'])) except (AssertionError, KeyError) as e: excel.write_data('add', case_info['case_id'] + 1, 8, '不通过') logger.error('第 {} 条用例不通过'.format(case_info['case_id'])) raise e
def test_recharge(self, case_info): method = case_info['method'] url = host + case_info['url'] headers = Handler().replace_data(case_info['headers']) headers = eval(headers) data = Handler().replace_data(case_info['data']) data = eval(data) expected_resp = eval(case_info['expected_resp']) actual_resp = visit_api(method, url, headers, json=data) leaveamount_after = MysqlHandlerMid().query(self.sql)['leave_amount'] try: for k, v in expected_resp.items(): self.assertEqual(actual_resp[k], v) if actual_resp['code'] == 0: self.assertTrue( self.leaveamount_before + Decimal(str(data['amount'])) == leaveamount_after) self.assertTrue(self.leaveamount_before + Decimal(str(data['amount'])) == Decimal( str(actual_resp['data']['leave_amount']))) query_data = MysqlHandlerMid().query( 'select * from futureloan.financelog where income_member_id = {} order by create_time desc;' .format(Handler.investor_id)) self.assertTrue(query_data) self.assertEqual(query_data['income_member_id'], Handler.investor_id) self.assertEqual(query_data['amount'], Decimal(str(data['amount']))) self.assertTrue(self.leaveamount_before + Decimal(str(data['amount'])) == query_data['income_member_money']) self.assertTrue(query_data['status'] == 1) self.assertTrue( int(str(datetime.now()).split(':')[1]) - int(str(query_data['create_time']).split(':')[1]) <= 1) else: # 异常用例,需查库验证,测试账户充值前后余额保持不变 self.assertEqual(self.leaveamount_before, leaveamount_after) excel.write_data('recharge', case_info['case_id'] + 1, 8, '通过') logger.info('第 {} 条用例通过'.format(case_info['case_id'])) except (AssertionError, KeyError) as e: excel.write_data('recharge', case_info['case_id'] + 1, 8, '不通过') logger.error('第 {} 条用例不通过'.format(case_info['case_id'])) raise e
def test_audit(self, case_info): method = case_info['method'] url = host + case_info['url'] headers = Handler().replace_data(case_info['headers']) headers = eval(headers) data = Handler().replace_data(case_info['data']) data = eval(data) sql = 'select `status` from futureloan.loan where id = {};'.format( data['loan_id']) if '*' not in case_info['title']: bid_info_before = MysqlHandlerMid().query(sql) expected_resp = eval(case_info['expected_resp']) actual_resp = visit_api(method, url, headers, json=data) try: for k, v in expected_resp.items(): self.assertEqual(actual_resp[k], v) if actual_resp['code'] == 0: bid_info = MysqlHandlerMid().query(sql) if data['approved_or_not'] is True: self.assertTrue(bid_info['status'] == 2) elif data['approved_or_not'] is False: self.assertTrue(bid_info['status'] == 5) else: if case_info['case_id'] in (5, 6, 7, 8, 9, 10): bid_info_after = MysqlHandlerMid().query(sql) self.assertEqual(bid_info_after['status'], bid_info_before['status']) excel.write_data('audit', case_info['case_id'] + 1, 8, '通过') logger.info('第{}条用例通过'.format(case_info['case_id'])) except (AssertionError, KeyError) as e: excel.write_data('audit', case_info['case_id'] + 1, 8, '不通过') logger.error('第{}条用例不通过'.format(case_info['case_id'])) raise e
def test_login(self, case_info): headers = eval(case_info['headers']) data = eval(case_info['data']) url = host + case_info['url'] expected_resp = eval(case_info['expected_resp']) actual_resp = visit_api(case_info['method'], url, headers, json=data) try: for k, v in expected_resp.items(): self.assertEqual(actual_resp[k], v) if actual_resp['code'] == 0: # 接口返回手机号应与登录手机号一致 self.assertEqual(actual_resp['data']['mobile_phone'], data['mobile_phone']) # 验证接口能正常返回 token 值 self.assertTrue(jsonpath(actual_resp, '$..token')[0]) # 验证 token 类型与说明文档一致 self.assertTrue( jsonpath(actual_resp, '$..token_type')[0] == 'Bearer') # 安全考虑,验证 token 失效时间 if int(str(datetime.now()).split(':')[1]) >= 55: self.assertTrue( int( jsonpath(actual_resp, '$..expires_in')[0].split( ':')[1]) + 55 == int(str(datetime.now()).split(':')[1])) else: self.assertTrue( int( jsonpath(actual_resp, '$..expires_in')[0].split( ':')[1]) == int(str(datetime.now()).split(':')[1]) + 5) excel.write_data('login', case_info['case_id'] + 1, 8, '通过') logger.info('第 {} 条用例通过'.format(case_info['case_id'])) # 添加 KeyError 异常处理:当接口返回缺少应有字段,也代表用例不能通过 except (AssertionError, KeyError) as e: excel.write_data('login', case_info['case_id'] + 1, 8, '不通过') logger.error('第 {} 条用例不通过'.format(case_info['case_id'])) raise e
def test_invest(self, case_info): method = case_info['method'] url = host+case_info['url'] if '#' in case_info['title']: self.setUp() headers = Handler().replace_data(case_info['headers']) headers = eval(headers) data = Handler().replace_data(case_info['data']) data = eval(data) expected_resp = eval(case_info['expected_resp']) actual_resp = visit_api(method, url, headers, json=data) # 查询投资账号投资后余额、投资/流水/回款记录条数 leaveamount_after = MysqlHandlerMid().query(self.sql_leaveamount)['leave_amount'] invest_count_after = MysqlHandlerMid().query(self.sql_count_invest)['count(id)'] financelog_count_after = MysqlHandlerMid().query(self.sql_count_financelog)['count(id)'] repayment_count_after = MysqlHandlerMid().query(self.sql_count_repayment)['count(r.id)'] try: for k, v in expected_resp.items(): self.assertEqual(actual_resp[k], v) if actual_resp['code'] == 0: # 一、验证投资账号:最新投资记录,数据库字段信息正确 sql = 'select * from futureloan.invest where member_id = {} order by create_time desc;'.format(data['member_id']) invest_info = MysqlHandlerMid().query(sql) self.assertTrue(invest_count_after == self.invest_count_before + 1) self.assertEqual(invest_info['loan_id'], data['loan_id']) self.assertEqual(invest_info['amount'], Decimal(str(data['amount']))) self.assertTrue(invest_info['is_valid'] == 1) self.assertTrue(int(str(datetime.now()).split(':')[1]) - int(str(invest_info['create_time']).split(':')[1]) <= 1) # 二、验证投资后:标状态信息变化正常 loan_info = MysqlHandlerMid().query('select status from futureloan.loan where member_id = {} order by create_time desc'.format(Handler.borrower_id)) # 非满标投资,标状态保持为 2 if '#' not in case_info['title']: self.assertTrue(loan_info['status'] == 2) # 满标投资,标状态变为 3 else: self.assertTrue(loan_info['status'] == 3) # 三、验证投资账号:余额变动正确 self.assertTrue(self.leaveamount_before-Decimal(str(data['amount'])) == leaveamount_after) # 四、验证投资账号:最新流水记录,数据库字段信息正确 financelog_info = MysqlHandlerMid().query('select * from futureloan.financelog where pay_member_id = {} order by create_time desc;'.format(data['member_id'])) self.assertTrue(financelog_count_after == self.financelog_count_before + 1) self.assertEqual(financelog_info['pay_member_id'], data['member_id']) self.assertEqual(financelog_info['income_member_id'], Handler.borrower_id) self.assertEqual(financelog_info['amount'], Decimal(str(data['amount']))) self.assertTrue(financelog_info['income_member_money'] == Decimal(str(0))) self.assertTrue(self.leaveamount_before - Decimal(str(data['amount'])) == financelog_info['pay_member_money']) self.assertTrue(financelog_info['status'] == 1) self.assertTrue(int(str(datetime.now()).split(':')[1]) - int(str(financelog_info['create_time']).split(':')[1]) <= 1) # 五、验证投资账号:投资标满标后,回款记录信息正确 if '#' not in case_info['title']: self.assertEqual(repayment_count_after, self.repayment_count_before) else: self.assertTrue(repayment_count_after == self.repayment_count_before + 3) else: # 验证异常投资:投资前后投资账户余额、投资/流水/回款记录条数不变 self.assertEqual(self.leaveamount_before, leaveamount_after) self.assertEqual(self.invest_count_before, invest_count_after) self.assertEqual(self.financelog_count_before, financelog_count_after) self.assertEqual(self.repayment_count_before, repayment_count_after) excel.write_data('invest', case_info['case_id'] + 1, 8, '通过') logger.info('第 {} 条用例通过'.format(case_info['case_id'])) except (AssertionError, KeyError) as e: excel.write_data('invest', case_info['case_id']+1, 8, '不通过') logger.error('第 {} 条用例不通过'.format(case_info['case_id'])) raise e