def test_update(self, case): method = case['method'] url = conf.get('env', 'base_url') + case['url'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') expected = eval(case['expected']) row = case['case_id'] + 1 if '#member_id#' in case['data']: # case['data']=case['data'].replace('#member_id#',self.member_id) data = eval(replace_data(case['data'])) response2 = request(method=method, json=data, url=url, headers=headers) res2 = response2.json() print('预期结果', expected) print('实际结果', res2) # # try: self.assertEqual(res2['code'], expected['code']) self.assertEqual(res2['msg'], expected['msg']) if case['check_sql']: sql = replace_data(case['check_sql']) reg_name1 = self.db.find_one(sql)['reg_name'] self.assertEqual(reg_name1, data['reg_name']) except AssertionError as e: log.error('用例----{}----执行不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='不通过') log.debug('预期结果', expected) log.debug('实际结果', res2) raise e else: log.info('测试用例----{}——----'.format(case['title'])) self.excel.write_data(row=row, column=8, value='通过')
def test_login(self,case): method=case['method'] url=conf.get('env','base_url')+(case['url']) headers=eval(conf.get('env','headers')) data=eval(case['data']) expected=eval(case['expected']) row=case['case_id']+1 response=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']) except AssertionError as e: log.error('用例--{}--执行不通过'.format(case['title'])) log.exception(e) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:{}'.format(res)) 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_info(self, case): pass method = case['method'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') if '#member_id#' in case['url']: case['url'] = replace_data(case['url']) url = conf.get('env', 'base_url') + case['url'] expected = eval(case['expected']) row = case['case_id'] + 1 response2 = request(method=method, headers=headers, url=url) res2 = response2.json() print('预期结果', expected) print('实际结果', res2) try: self.assertEqual(expected['code'], res2['code']) self.assertEqual(expected['msg'], res2['msg']) except AssertionError as e: log.error('用例----{}----执行不通过'.format(case['title'])) log.exception(e) log.debug('预期结果', expected) log.debug('实际结果', res2) self.excel.write_data(row=row, column=8, value='不通过') raise e else: log.info('用例----{}----执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='通过')
def test_loans(self, case): url = conf.get('env', 'base_url') + case['url'] method = case['method'] headers = eval(conf.get('env', 'headers')) # headers['Authrization']=getattr(EnvData,'token') data = eval(case['data']) expected = eval(case['expected']) row = case['case_id'] + 1 response2 = request(method=method, params=data, url=url, headers=headers) res2 = response2.json() print('预期结果:', expected) print('实际结果:', res2) try: self.assertEqual(expected['code'], res2['code']) self.assertEqual(expected['msg'], res2['msg']) # 可以进行返回数据条数的校验 self.assertEqual(expected['len'], len(res2['data'])) except AssertionError as e: log.error('用例----{}----执行不通过'.format(case['title'])) log.exception(e) log.debug('预期结果', expected) log.debug('实际结果', res2) self.excel.write_data(row=row, column=8, value='不通过') raise e else: log.info('用例----{}----执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='通过')
def test_recharge(self, case): # pass url = conf.get('env', 'base_url') + case['url'] method = case['method'] # 1.准备用例数据 # 替换用户id,转换为字典 data = eval(replace_data(case['data'])) # 2.准备请求头 headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') expected = eval(case['expected']) row = case['case_id'] + 1 # 判断该用例是否需要数据库校验 if case['check_sql']: sql = replace_data(case['check_sql']) res_db = self.db.find_one(sql) start_money = res_db['leave_amount'] print('充值前金额:', start_money) # 2.发送请求,获取实际结果 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']) res_db = self.db.find_one(sql) end_money = res_db['leave_amount'] print('充值后金额:', end_money) # # 3.断言预期结果和实际结果 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) if case['check_sql']: # 将充值金额转换为decimal类型,因为数据库是decimal类型的 recharge_money = decimal.Decimal(str(data['amount'])) self.assertEqual(recharge_money, end_money - start_money) except AssertionError as e: log.error('用例--{}--执行不通过'.format(case['title'])) log.exception(e) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:{}'.format(res)) self.excel.write_data(row=row, column=8, value='fail') raise e else: log.info('用例--{}--执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='pass')
def test_audit(self, case): # 1.准备数据 url = conf.get('env', 'base_url') + case['url'] if '#pass_loan_id#' in case['data']: data = eval(replace_data(case['data'])) # data = eval(case['data'].replace('#loan_id#', self.loan_id)) data = eval(replace_data(case['data'])) headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'admin_token') if case['title'] == '普通用户审核': headers['Authorization'] = getattr(EnvData, 'token') method = case['method'] expected = eval(case['expected']) row = case['case_id'] + 1 response = request(method=method, url=url, json=data, headers=headers) res = response.json() print(res) print(expected) if case['title'] == '审核通过' and case['case_id'] == 1: AuditTestCase.pass_loan_id = data['loan_id'] # 3.断言 try: self.assertEqual(res['code'], expected['code']) self.assertEqual(res['msg'], expected['msg']) if case['check_sql']: # 原方法 # case['check_sql'] = case['check_sql'].replace('#loan_id#',self.loan_id) # res = self.db.find_one(case['check_sql']) # res2 = res['status'] # self.assertEqual(res2, expected['status']) # 新方法 sql = replace_data(case['check_sql']) # case['check_sql'].replace('#loan_id#',self.loan_id) status = self.db.find_one(sql)['status'] self.assertEqual(status, expected['status']) except AssertionError as e: log.error('用例----{}----执行不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') log.debug('预期结果', expected) log.debug('实际结果', res) raise e else: log.info('用例----{}----执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='pass')
def __init__(self): '''初始化方法中,连接到数据库''' # 建立连接 self.con = pymysql.connect( host=conf.get('mysql', 'host'), port=conf.getint('mysql', 'port'), # 端口是数值,不是字符创 user=conf.get('mysql', 'user'), password=conf.get('mysql', 'password'), charset='utf8', cursorclass=pymysql.cursors.DictCursor # 定义游标类型,默认为元组类型,改为返回数据为字典 ) # 创建一个游标对象 self.cur = self.con.cursor()
def invest_login(): url = conf.get('env', 'base_url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'invest_phone'), 'pwd': conf.get('test_data', 'invest_pwd') } headers = eval(conf.get('env', 'headers')) response = request(method='post', url=url, json=data, headers=headers) res = response.json() invest_member_id = str(jsonpath(res, '$..id')[0]) invest_token = 'Bearer' + ' ' + jsonpath(res, '$..token')[0] setattr(EnvData, 'invest_member_id', invest_member_id) setattr(EnvData, 'invest_token', invest_token)
def test_withdraw(self, case): url = conf.get('env', 'base_url') + case['url'] method = case['method'] # case['data']=case['data'].replace('#member_id#',self.member_id) # data=eval() data = eval(replace_data(case['data'])) headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') expected = eval(case['expected']) row = case['case_id'] + 1 if case['check_sql']: case['check_sql'] = replace_data(case['check_sql']) res_before = self.db.find_one(case['check_sql']) money_before = res_before['leave_amount'] print('提现前金额', money_before) response1 = request(method=method, url=url, headers=headers, json=data) res1 = response1.json() print('预期结果', expected) print('实际结果', res1) if case['check_sql']: case['check_sql'] = replace_data(case['check_sql']) res_before = self.db.find_one(case['check_sql']) money_after = res_before['leave_amount'] print('提现后金额', money_after) try: self.assertEqual(expected['code'], res1['code']) self.assertEqual(expected['msg'], res1['msg']) if case['check_sql']: self.assertEqual(decimal.Decimal(str(data['amount'])), money_before - money_after) except AssertionError as e: log.error('用例----{}----执行不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') log.debug('预期结果', expected) log.debug('实际结果', res1) raise e else: log.info('用例----{}----执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='pass')
def test_add(self, case): url = conf.get('env', 'base_url') + case['url'] method = case['method'] headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') # 替换用例中的member_Id # case['data']=case['data'].replace('#member_id#',self.member_id) data = eval(replace_data(case['data'])) expected = eval(case['expected']) 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.find_count(sql) response2 = request(method=method, headers=headers, json=data, url=url) res2 = response2.json() print('预期结果', expected) print('实际结果', res2) try: self.assertEqual(expected['code'], res2['code']) self.assertEqual(expected['msg'], res2['msg']) # 加标之后 if case['check_sql']: sql = replace_data(case['check_sql']) end_count = self.db.find_count(sql) print(end_count - start_count) # self.assertEqual(1,end_count-start_count) except AssertionError as e: log.error('用例----{}----执行不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='不通过') log.debug(expected) log.debug(res2) raise e else: log.info('用例----{}----执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='通过')
class Login_setup(): headers = eval(conf.get('env', 'headers')) url = conf.get('env', 'base_url') + '/member/login' data = { 'mobile_phone': conf.get('test_data', 'phone'), 'pwd': conf.get('test_data', 'pwd') } # @classmethod def login(self): response1 = request(method='post', headers=self.headers, json=self.data, url=self.url) res1 = response1.json() member_id = str(jsonpath(res1, '$..id')[0]) token1 = jsonpath(res1, '$..token')[0] return member_id, token1
def setUp(self): # 每条用例执行的前置条件:添加一个新的项目 url = conf.get('env', 'base_url') + '/loan/add' headers = eval(conf.get('env', 'headers')) headers['Authorization'] = getattr(EnvData, 'token') data = { "member_id": getattr(EnvData, 'member_id'), "title": "贷款买楼666", "amount": 1000, "loan_rate": 1, "loan_term": 1, "loan_date_type": 1, "bidding_days": 1 } # 发送请求,添加项目 response = request(method='post', url=url, json=data, headers=headers) res = response.json() loan_id = str(jsonpath(res, '$..id')[0]) setattr(EnvData, 'loan_id', loan_id)
def handlerlogging(): # 创建日志收集器;设置日志收集器等级 logcollect = logging.getLogger('zixia') logcollect.setLevel(conf.get('log', 'level')) # conf.get('log', 'level') # 创建输出到控制台渠道;控制台等级;控制台添加到日志收集器 sh = logging.StreamHandler() sh.setLevel(conf.get('log', 'sh_level')) logcollect.addHandler(sh) # 创建输出文件渠道;文件等级;文件添加到日志收集器 fh = logging.FileHandler(log_filepath, encoding='utf8') fh.setLevel(conf.get('log', 'fh_level')) logcollect.addHandler(fh) # 设置日志格式 formats = '%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s' format = logging.Formatter(formats) sh.setFormatter(format) fh.setFormatter(format) return logcollect
def replace_data(data): ''' 替换数据 :param data: :return: ''' while re.search('#(.*?)#', data): res = re.search('#(.*?)#', data) key = res.group() item = res.group(1) try: # 获取配置文件中对应的值 value = conf.get('test_data', item) except: # 去EnvData类里面获取对应的属性(环境变量) value = getattr(EnvData, item) data = data.replace(key, value) return data
def test_register(self, case): # 第一步:准备用例数据 # 请求方法 method = case['method'] # 请求地址 url = conf.get('env', 'base_url') + case['url'] # 请求参数 # 判断是否有手机号码需要替换 if '#phone#' in case['data']: # 随机生成一个手机号码 phone = self.random_phone() # 将参数中的#phone#替换成随机生成的手机号 case['data'] = case['data'].replace('#phone#', phone) data = eval(case['data']) # 请求头 配置文件的请求头不能换行 headers = eval(conf.get('env', 'headers')) expected = eval(case['expected']) row = case['case_id'] + 1 # 第二步:发送请求,获取实际结果 response = request(method=method, url=url, json=data, headers=headers) res = response.json() # print('实际结果:',res) # print(expected['code']) # 用print可以显示详细信息在报告上 print('预期结果', expected) print('实际结果', res) # 第三步:断言 try: self.assertEqual(expected['code'], res['code']) self.assertEqual(expected['msg'], res['msg']) # 判断是否需要sql校验 if case['check_sql']: sql = case['check_sql'].replace('#phone#', data['mobile_phone']) # 方法1:是否可以找到数据 # res=self.db.find_one(sql) # self.assertTrue(res) #none为false?? # 方法2:是否可以找到一条数据 res = self.db.find_count(sql) self.assertEqual(1, res) except AssertionError as e: log.error('用例--{}--执行不通过'.format(case['title'])) log.exception(e) log.debug('预期结果:{}'.format(expected)) log.debug('实际结果:{}'.format(res)) self.excel.write_data(row=row, column=8, value='fail') raise e else: # log.info('用例--{}--执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='pass')
def test_invest(self, case): # 1.准备数据 url = conf.get('env', 'base_url') + case['url'] method = case['method'] headers = eval(conf.get('env', 'headers')) if case['interface'] != 'login': # 如果不是登陆接口,则添加一个token headers['Authorization'] = getattr(EnvData, 'token') data = eval(replace_data(case['data'])) expected = eval(case['expected']) row = case['case_id'] + 1 # 获取需要sql校验的数据: if case['check_sql']: sql1 = 'select leave_amount from futureloan.member where member_id = #member_id#'.replace( '#member_id#', getattr(EnvData, 'member_id')) sql2 = 'select * from futureloan.invest where member_id= #member_id#;'.replace( '#member_id#', getattr(EnvData, 'member_id')) sql3 = 'select * from futureloan.financelog where pay_member_id= #member_id#;'.replace( getattr(EnvData, 'member_id')) start_amount = self.sql.find_one(sql1) start_invest_num = self.sql.find_count(sql2) start_financelog_num = self.sql.find_count(sql3) # 2.发送请求,获取实际结果 response = request(url=url, json=data, method=method, headers=headers) res = response.json() # 登录需要提取Id和token # 加标需要提取标id if case['interface'] == 'login': # 如果是登录接口,需要提取member_id 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 loan_id = str(jsonpath(res, '$..id')[0]) setattr(EnvData, 'loan_id', loan_id) # 3.断言 try: self.assertEqual(res['code'], expected['code']) self.assertEqual(res['msg'], expected['msg']) if case['check_sql']: sql1 = 'select leave_amount from futureloan.member where member_id = #member_id#'.replace( '#member_id#', getattr(EnvData, 'member_id')) sql2 = 'select * from futureloan.invest where member_id= #member_id#;'.replace( '#member_id#', getattr(EnvData, 'member_id')) sql3 = 'select * from futureloan.financelog where pay_member_id= #member_id#;'.replace( '#member_id#', getattr(EnvData, 'member_id')) after_amount = self.sql.find_one(sql1) self.assertEqual(after_amount - start_amount, Decimal(str(data['amount']))) after_invest_num = self.sql.find_count(sql2) self.assertEqual(after_invest_num - start_invest_num, 1) after_financelog_num = self.sql.find_count(sql3) self.assertEqual( after_financelog_num - start_financelog_num, 1) if '满标' in case['title']: sql4 = 'select id from futureloan.invest where loan_id= {};'.format( getattr(EnvData, 'loan_id')) invest_ids = self.sql.find_all(sql4) for i in invest_ids: sql5 = 'select * from futureloan.repayment where invest_id= {};'.format( i) repayment_num = self.sql.find_count(sql5) self.assertTrue(repayment_num) # if case['check_sql']: # # sql = case['check_sql'].replace('#loan_id#', self.loan_id) # status = self.db.find_one(sql)['status'] # self.assertEqual(status, expected['status']) except AssertionError as e: log.error('用例----{}----执行不通过'.format(case['title'])) log.exception(e) self.excel.write_data(row=row, column=8, value='fail') log.debug('预期结果', expected) log.debug('实际结果', res) raise e else: log.info('用例----{}----执行通过'.format(case['title'])) self.excel.write_data(row=row, column=8, value='pass')
''' author:紫夏 Time:2020/3/15 23:34 ''' ''' 1、将封装好上课创建日志收集器的函数 2、将之前注册函数用例方法中将执行的结果记录到日志文件中 (通过的用例记录info等级的日志,没通过的用例记录error等级的日志) ''' import logging from common.handler_config import conf import os from common.handler_path import LOG_DIR log_filepath = os.path.join(LOG_DIR, conf.get('log', 'filename')) def handlerlogging(): # 创建日志收集器;设置日志收集器等级 logcollect = logging.getLogger('zixia') logcollect.setLevel(conf.get('log', 'level')) # conf.get('log', 'level') # 创建输出到控制台渠道;控制台等级;控制台添加到日志收集器 sh = logging.StreamHandler() sh.setLevel(conf.get('log', 'sh_level')) logcollect.addHandler(sh) # 创建输出文件渠道;文件等级;文件添加到日志收集器 fh = logging.FileHandler(log_filepath, encoding='utf8') fh.setLevel(conf.get('log', 'fh_level'))