def request(self, method, url, data=None): method = method.upper() # 将字符转换成大写 增强程序的健壮性 config = ReadConfig() pre_url = config.get("api", "pre_url") url = pre_url + url if data is not None and type(data) == str: #python中json和eval函数的区别: #1 json是跨语言的,而eval是python的函数,是基于python的格式进行转换的,用eval函数,只能是转换python格式的数据类型,eval的作用范围比较小,而eval函数的作用范围比较大,json的跨语言的数据格式 # data = eval(data) #如果是字符串就转回字典 data = json.loads(data) #如果是字符串就转回字典 logger.info('method: {0} url: {1}'.format(method, url)) logger.info('data: {0}'.format(data)) if method == 'GET': resp = self.session.request( method, url=url, params=data) # 调用get方法,使用params传参(url传参) logger.info('response: {0}'.format(resp.text)) return resp elif method == 'POST': resp = self.session.request(method, url=url, data=data) # 调用post方法,使用data传参(表单传参) logger.info('response: {0}'.format(resp.text)) return resp else: logger.info('un-support method!')
def request(self, method, url, data=None): method = method.upper() # 将字符串全部转化成大写 if data is not None and type( data) == str: # 从表格中读取的data是字符串类型,而request里需要字典 data = eval(data) # 将字符串转化成字典 # data = json.loads(data) #将json格式的字符串转化为字典 logger.info('url: {0}'.format(url)) config = ReadConfig() pre_url = config.get('api', 'pre_url') url = pre_url + url #URL拼接 logger.info('method: {0} 拼接后url: {1}'.format(method, url)) logger.info('data: {0}'.format(data)) # print('data: {0}'.format(data),type(data)) if method == 'GET': resp = self.session.request(method, url=url, params=data) # 调用get方法,使用params传参 logger.info('response: {0}'.format(resp.text)) return resp elif method == 'POST': resp = self.session.request(method, url=url, data=data) # 调用post方法,使用data传参 logger.info('response: {0}'.format(resp.text)) return resp else: logger.error('Un-support method !!!')
def request(self, method, url, data=None): # 因为共享session,所以这里不需要传cookie了 # 引入配置文件 拼接url config = ReadConfig() pre_url = config.get('api', 'pre_url') url = pre_url + url method = method.upper() # 将字符串全改成大写,兼容性 if data is not None and type(data) == str: data = eval(data) # 如果是字符串需转字典。否则运行结果为:...'手机号码不能为空' print('method:{0} url:{1}'.format(method, url)) print('data:{0}'.format(data)) if method == 'GET': # return self.session.request(method, url=url, params=data) # 注意方式params,data要传字典 resp = self.session.request(method, url=url, params=data) print('response:{0}'.format(resp.text)) return resp elif method == 'POST': # return self.session.request(method, url=url, data=data) # 注意方式data,data要传字典 resp = self.session.request(method, url=url, data=data) print('response:{0}'.format(resp.text)) return resp else: print('Un-support method !!!')
def test_run_cases(self,case): print("执行第{0}条测试用例,测试title是:{1}".format(case['id'],case['title'])) #调用读取配置文件类 read_config = ReadConfig() url = read_config.get('api', 'pre_url') +case['url']#URL进行拼接 #查找参数化的测试数据,进行动态替换 data=context.replace(case['data']) # 使用封装好的HttpRequest类中的http_request方法来完成请求 res=self.request.http_request(case['method'],url,data) #断言 try: self.assertEqual(case['ExpectedResult'],int(res.json()['code'])) print("测试结果:Pass") TestResult = 'Pass' #判断是否加标成功,如果成功就按照借款人的ID去数据库查询最新的标记录 if res.json()['msg'] == '加标成功': loan_member_id = getattr(Context, 'loan_member_id') sql = "select id from future.loan where memberID='{0}'" \ " order by createTime desc limit 1".format(loan_member_id) loan_id = self.mysql.fetch_one(sql)[0] setattr(Context, 'loan_id',str(loan_id)) # 转成str,后续通过正则替换 except Exception as e: print("执行接口测试期望结果与实际结果不一致:{0}".format(e)) print("测试结果:Fail") TestResult = 'Fail' raise e finally: DoExcel(contants.cases_file, 'invest').write_back(case['id'] + 1, 7,str(res.json()['code'])) # 写入实际结果 DoExcel(contants.cases_file, 'invest').write_back(case['id'] + 1, 8, TestResult) # 写入测试结论
def __init__(self): # 想想这块如何放到配置文件里面去??? # host = "test.lemonban.com" # user = "******" # password = "******" config = ReadConfig() host = config.get('db', 'host') user = config.get('db', 'user') password = config.get('db', 'pwd') port = config.getint('db', 'port') # 1,建立连接 self.mysql = pymysql.connect(host=host, user=user, password=password, port=port) # 2,新建一个查询 self.cursor = self.mysql.cursor()
def __init__(self, return_dict=False): # 想想这块如何放到配置文件里面去??? # host = "test.lemonban.com" # user = "******" # password = "******" config = ReadConfig() host = config.get('db', 'host') user = config.get('db', 'user') password = config.get('db', 'pwd') port = config.getint('db', 'port') # 1,建立连接 self.mysql = pymysql.connect(host=host, user=user, password=password, port=port) # 2,新建一个查询 if return_dict: self.cursor = self.mysql.cursor(pymysql.cursors.DictCursor) # 指定每行数据以字典的形式返回 else: self.cursor = self.mysql.cursor() # 指定每行数据以元祖的形式返回
def request(self, method, url, data=None): method = method.upper() # 将字符串全部转成大写 config = ReadConfig() value = config.get('api', 'pre_url') url = value + url if data is not None and type(data) == str: data = json.loads(data) logger.info('method:{0} url:{1}'.format(method, url)) logger.info('data:{0}'.format(data)) if method == 'GET': resp = self.session.request(method, url=url, params=data) logger.info('response:{0}'.format(resp.text)) return resp elif method == 'POST': resp = self.session.request(method, url=url, data=data) logger.info('response:{0}'.format(resp.text)) return resp else: logger.error('unsupport method')
def test_run_cases(self,case): print("执行第{0}条测试用例,测试title是:{1}".format(case['id'],case['title'])) #调用读取配置文件类 read_config = ReadConfig() url = read_config.get('api', 'pre_url') +case['url']#URL进行拼接 # 使用封装好的HttpRequest类中的http_request方法来完成请求 res=self.request.http_request(case['method'],url,case['data']) #断言 try: self.assertEqual(eval(case['ExpectedResult']),res.json()) print("测试结果:Pass") TestResult = 'Pass' except Exception as e: print("执行接口测试期望结果与实际结果不一致:{0}".format(e)) print("测试结果:Fail") TestResult = 'Fail' raise e finally: DoExcel(contants.cases_file, 'recharge').write_back(case['id'] + 1, 7, str(res.json())) # 写入实际结果 DoExcel(contants.cases_file, 'recharge').write_back(case['id'] + 1, 8, TestResult) # 写入测试结论
def test_invest(self,case): readconfig = ReadConfig() pre_url = readconfig.get("api","pre_url") print("开始执行第{}个测试用例:".format(case.case_id)) case.data = replace(case.data) resp = self.request.request(method=case.method,url = pre_url+case.url,data=case.data) doexcels = doexcel.DoExcel(contants.case_file, "invest") try: self.assertEqual(case.expected,resp.text) doexcels.write_back(row=case.case_id + 2, col=8, value="PASS", sheet_name="invest") #加一层判断,判断加标是否成功,如果成功,就把标的的ID查出来反射给类的属性,以便下一条用例取 if resp.json()["msg"] == "加标成功": load_member_id = getattr(Context,"loan_member_id") mysql = MysqlUtil() sql = "select Id from future.loan where MemberID={} order by CreateTime desc limit 1".format(load_member_id) loan_id = mysql.fetchone(sql)[0] setattr(Context,"loan_id",str(loan_id)) except AssertionError as e: doexcels.write_back(row=case.case_id + 2, col=8, value="FAIlED", sheet_name="invest") doexcels.write_back(case.case_id + 2,7,resp.text,sheet_name="invest") print(resp.text)
def request(self, method, url, data=None): method = method.upper() # 将字符转成全部大写 config = ReadConfig() pre_url = config.get('api', 'pre_url') # 拼接 url = pre_url + url # URL拼接 if data is not None and type(data) == str: data = eval(data) # 如果是字符串就转成字典 logger.info('method: {0} url: {1}'.format(method, url)) logger.info('data: {0}'.format(data)) if method == 'GET': resp = self.session.request(method, url=url, params=data) # 调用get方法,使用params传参 # log.info('response: {0}'.format(resp.text)) return resp elif method == 'POST': resp = self.session.request(method, url=url, data=data) # 调用post方法,使用data传参 logger.info('response: {0}'.format(resp.text)) return resp else: logger.error('Un-support method !!!') pass
def test_addloan(self, case): readconfig = ReadConfig() pre_url = readconfig.get("api", "pre_url") print("开始执行第{}个测试用例:".format(case.case_id)) resp = self.request.request(method=case.method, url=pre_url + case.url, data=case.data) doexcels = doexcel.DoExcel(contants.case_file, "addloan") try: self.assertEqual(case.expected, resp.text) doexcels.write_back(row=case.case_id + 2, col=8, value="PASS", sheet_name="addloan") except AssertionError as e: doexcels.write_back(row=case.case_id + 2, col=8, value="FAIlED", sheet_name="addloan") doexcels.write_back(case.case_id + 2, 7, resp.text, sheet_name="addloan") print(resp.text)
def __init__(self): config = ReadConfig() self.in_level = config.get('log', 'in_level') self.out_level = config.get('log', 'out_level') self.file_level = config.get('log', 'file_level') self.formatter = config.get('log', 'formatter')
""" @Time :2019/2/2010:56 @Author :pengpeng @Email :[email protected] @File :logger.py @Software :PyCharm @Function : """ import logging import logging.handlers from common import contants from common.config import ReadConfig config = ReadConfig() logger_level = config.get('log', 'logger_level') file_handler_level = config.get('log', 'file_handler_level') console_handler_level = config.get('log', 'console_handler_level') def get_logger(logger_name): logger = logging.getLogger(logger_name) logger.setLevel(logger_level) # 定义输出格式 fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s]" formate = logging.Formatter(fmt) file_handler = logging.handlers.RotatingFileHandler(contants.case_log, maxBytes=20 * 1024 * 1024,