class BaseHttp(): GetLog = Log('basehttp') getlog = GetLog.log() getconfig = operationConfig.CONFIG() scheme = getconfig.get_config_value('HTTP', 'scheme') baseurl = getconfig.get_config_value('HTTP', 'baseurl') port = getconfig.get_config_value('HTTP', 'port') timeout = getconfig.get_config_value('HTTP', 'timeout') if port: baseurl = scheme + '://' + baseurl + ':' + port else: baseurl = scheme + '://' + baseurl getlog.info('读取url成功:{0}'.format(baseurl)) def get(self, uri, params): headers = {'Content-type': 'application/x-www-form-urlencoded'} url = self.baseurl + uri self.getlog.info('最终拼接地址为:{0}'.format(url)) try: response = requests.get(url, headers=headers, params=params) self.getlog.info('get请求成功,内容为:%s' % response.content) return response except TimeoutError as e: self.getlog.error('get请求报错了,报错为:%s' % e) def post(self, uri, data): headers = {'Content-type': 'application/x-www-form-urlencoded'} url = self.baseurl + uri self.getlog.info('最终拼接地址为:{0}'.format(url)) try: response = requests.post(url, headers=headers, data=data, timeout=int(self.timeout)) self.getlog.info('post请求成功,内容为:%s' % response.text) if response.status_code == 200: self.getlog.info('post请求成功') else: self.getlog.info('post请求返回失败') return response except TimeoutError as e: self.getlog.error('post请求报错了,报错为:%s' % e) def post_with_json(self, uri, data): headers = {'Content-type': 'application/json'} url = self.baseurl + uri try: response = requests.post(url, headers=headers, json=data, timeout=int(self.timeout)) self.getlog.info('post_with_json请求成功,内容为:%s' % response.text) return response except TimeoutError as e: self.getlog.error('post_with_json请求报错了,报错为:%s' % e)
def __init__(self): GetLog = Log('basecode') self.getlog = GetLog.log() LOCALCONFIG = operationConfig.CONFIG() pro_dir = operationConfig.RROJECT_DIR excel_dir = os.path.join( pro_dir, LOCALCONFIG.get_config_value('EXCEL', 'excel_file')) self.baseexcel = BaseExcel(excel_dir) self.dict_key_value = eval( LOCALCONFIG.get_config_value('EXCEL', 'excel_case_name')) self.getlog.info('案例key值读取成功为:{0}'.format(self.dict_key_value))
# -*- encoding: utf-8 -*- import os import time import operationConfig from base.log import Log config = operationConfig.CONFIG() report_dir = os.path.join(operationConfig.RROJECT_DIR, 'report\{0}'.format( time.strftime('%Y%m%d'))) #报告地址report下的当前日期下 set_value = config.set_config_value( 'REPORT', 'path', '{0}'.format(report_dir)) #将报告地址写入ini文件中,log记录日志的地址也是该目录下 if not os.path.exists(report_dir): os.mkdir(report_dir) GetLog = Log('Run') getlog = GetLog.log() result = os.system('pytest RunCode --html={0}/report{1}.html'.format( report_dir, time.strftime('%H%M%S'))) getlog.info('报告执行结果查看执行报告')
# Log.setLevel(logging.DEBUG) # # filelog = logging.FileHandler('aa.log') # screenlog=logging.StreamHandler() # # style = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)s') # # filelog.setFormatter(style) # screenlog.setFormatter(style) # # Log.addHandler(filelog) # Log.addHandler(screenlog) # # Log.error('dsdsfd') # Log.info('23456') Config = operationConfig.CONFIG() #获得定义好的get_config_value方法 class Log(): def __init__(self, servername='root'): self.servername = servername self.log1 = logging.getLogger(self.servername) self.log1.setLevel(logging.DEBUG) logaddress = os.path.join(Config.get_config_value('REPORT', 'path'), 'system.log') ''' 读取config.ini中的report-path,通过Run.py文件写入path,每天更新一个路径 ''' filelog = logging.FileHandler(logaddress) screenlog = logging.StreamHandler()
import os import operationConfig as opconfig import time from base.log import Log # file=os.popen('pip freeze >>requirment.txt') # # file.read() # os.system('pip freeze >>requirment1.txt') config=opconfig.CONFIG() report_dir=os.path.join(opconfig.PROJECT_DIR,'report\{0}'.format(time.strftime('%Y%m%d'))) # print(report_dir) config.set_config_value('REPORT','path',report_dir) report_dir=config.get_config_value('REPORT','path') if not os.path.exists(report_dir): os.mkdir(report_dir) Getlog=Log('Run') getlog=Getlog.log() getlog.info('报告和日志文件父路径创建成功') # file=os.popen('pytest Runcode --html={0}/测试报告.html'.format(report_dir)) # file.read()#此方法有时会有乱码,因编码问题不能read() os.system('pytest Runcode --html={0}/测试报告.html'.format(report_dir)) getlog.info('案例执行完,请查看({0})测试报告'.format(report_dir))