标签:dict star exce edr 列表 from mode cto import
该自动化测试框架命名为:ATP,bin目录下写主程序,cases目录下放测试用例,conf目录下放配置文件,lib目录下放各个封装好的模块,logs目录下放日志文件,和readme文件。
common.py:封装读取excel用例、调用接口、检验结果、写入报告这几个模块。
1 """ 2 第一步:读取excel中用例 3 第二步:根据用例发送请求 4 第三步:校验结果 5 第四步:将测试结果、返回报文写入excel 6 """ 7 import xlrd,requests 8 from xlutils import copy 9 from lib.log import atp_log 10 11 class OpCase(object): 12 def get_case(self,file_path): 13 cases= [] #定义一个列表存放所有的cases 14 if file_path.endswith(‘.xls‘) or file_path.endswith(‘.xlsx‘): 15 try: 16 book = xlrd.open_workbook(file_path) 17 sheet = book.sheet_by_index(0) 18 for i in range(1,sheet.nrows): 19 row_data = sheet.row_values(i) #获取的每一行数据存到列表row_data 20 cases.append(row_data[4:8]) 21 atp_log.info(‘共读取%s条用例‘%(len(cases))) 22 self.file_path = file_path #因为该函数已经传了参数路径,为方便write_excel引用,在此实例化 23 except Exception as e: 24 atp_log.error(‘[%s]用例获取失败,错误信息:%s‘%(file_path,e)) 25 else: 26 atp_log.error(‘用例文件不合法,%s‘%file_path) 27 return cases 28 def my_request(self,url,method,data): 29 data = self.dataToDict(data) 30 try: 31 if method.upper() == ‘POST‘: 32 res = requests.post(url,data).text 33 elif method.uper() == ‘GET‘: 34 res = requests.get(url,params=data).text 35 else: 36 atp_log.warning(‘该请求方式暂不支持‘) 37 res = ‘该请求方式暂不支持‘ 38 except Exception as e: 39 msg = ‘【%s】接口调用失败,%s‘%(url,e) 40 atp_log.error(msg) 41 res = msg 42 return res 43 def dataToDict(self,data): #把数据转成字典。 44 res = {} 45 data = data.split(‘,‘) 46 for d in data: # 47 k, v = d.split(‘=‘) 48 res[k] = v 49 def check_res(self,res,check): #res:实际结果,check:预期结果 50 res = res.replace(‘": "‘,‘=‘).replace(‘": ‘,‘=‘) 51 for c in check.split(‘,‘): 52 if c not in res: 53 atp_log.info(‘结果校验失败,预期结果:【%s】,实际结果【%s】‘%(c,res)) 54 return ‘失败‘ 55 return ‘成功‘ 56 def write_excel(self,case_res): 57 book = xlrd.open_workbook(self.file_path) 58 new_book = copy.copy(book) 59 sheet = new_book.get_sheet(0) 60 row = 1 61 for case_case in case_res: 62 sheet.write(row,8,case_case[0]) 63 sheet.write(row,9,case_case[1]) 64 row += 1 65 new_book.save(self.file_path.replace(‘xlsx‘,‘xls‘))
log.py:封装日志模块
1 import logging,os 2 from logging import handlers 3 from conf import setting 4 class Mylogger(): 5 def __init__(self,file_name,level=‘info‘,backCount=5,when=‘D‘): 6 logger = logging.getLogger() # 先实例化一个logger对象,先创建一个办公室 7 logger.setLevel(self.get_level(level)) # 设置日志的级别 8 # f1 = logging.FileHandler(filename=‘a.log‘,mode=‘a‘,encoding=‘utf-8‘) #找到写日志文件的这个人 9 c1 = logging.StreamHandler() # 负责往控制台输出的 10 b1 = handlers.TimedRotatingFileHandler(filename=file_name, when=when, interval=1, backupCount=backCount, encoding=‘utf-8‘) 11 fmt = logging.Formatter(‘%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s‘) 12 c1.setFormatter(fmt) 13 b1.setFormatter(fmt) 14 logger.addHandler(c1) 15 logger.addHandler(b1) 16 self.logger = logger 17 def get_level(self,str): 18 level = { 19 ‘debug‘:logging.DEBUG, 20 ‘info‘:logging.INFO, 21 ‘warm‘:logging.WARNING, 22 ‘error‘:logging.ERROR 23 } 24 str = str.lower() 25 return level.get(str) 26 27 path = os.path.join(setting.LOG_PATH,setting.LOG_NAME) 28 atp_log = Mylogger(path,‘debug‘).logger 29 #直接在这里实例化,用的时候不用再实例化了 30 #别的地方用的时候,直接atp_log.warnning(‘xxxx‘)
send_mail.py:封装发送邮件模块
1 import yagmail 2 from conf import setting 3 from lib.log import atp_log 4 def sendmail(title,content,attrs=None): 5 m = yagmail.SMTP(host=setting.MAIL_HOST,user=setting.MAIL_USER, 6 password=setting.MAIL_PASSWRD,smtp_ssl=True) 7 m.send(to=setting.TO, 8 subject=title, 9 contents = content, 10 attachments = attrs) 11 atp_log.info(‘发送邮件完成‘)
setting.py,配置文件:设置邮件地址、日志默认级别、用例存放路径、日志存放路径、日志文件名
1 import os 2 BASE_PATH = os.path.dirname( 3 os.path.dirname(os.path.abspath(__file__)) 4 ) #三层目录定位到ATP目录 5 MAIL_HOST = ‘smtp.qq.com‘ 6 MAIL_USER=‘12*****89@qq.com‘ 7 MAIL_PASSWRD = ‘gjn*****bcgh‘ 8 TO = [ 9 ‘12*****9@qq.com‘ 10 ] 11 LEVEL = ‘debug‘ #设置日志默认级别 12 13 LOG_PATH = os.path.join(BASE_PATH,‘logs‘) #日志文件在logs目录下 14 CASE_PATH = os.path.join(BASE_PATH,‘cases‘) #用例文件在cases目录下 15 LOG_NAME = ‘atp_log‘ #设置日志文件名
start.py
1 import os,sys 2 BASE_PATH = os.path.dirname( 3 os.path.dirname(os.path.abspath(__file__)) 4 ) 5 sys.path.insert(0,BASE_PATH) 6 7 from lib.common import OpCase 8 from lib.send_mail import sendmail 9 from conf import setting 10 class CaseRun(object): 11 def find_case(self): 12 op = OpCase() 13 for f in os.listdir(setting.CASE_PATH): #每次循环的时候读一个excel 14 abs_path = os.path.join(setting.CASE_PATH,f) 15 case_list = op.get_case(abs_path) 16 res_list = [] 17 pass_count,fail_count= 0,0 18 for case in case_list: #循环每一个excel里面的所有用例 19 url,method,req_data,check = case 20 res = op.my_request(url,method,req_data) #调用完接口返回的结果 21 status = op.check_res(res,check) 22 res_list.append([res,status]) 23 if status == ‘通过‘: 24 pass_count += 1 25 else: 26 fail_count += 1 27 op.write_excel(res_list) 28 msg = ‘‘‘ 29 xx你好, 30 本次共运行%s条用例,通过%s条,失败%s条。 31 ‘‘‘%(len(res_list),pass_count,fail_count) 32 sendmail(‘测试用例运行结果‘,content=msg,attrs=abs_path) 33 34 CaseRun().find_case()
OK,数据驱动自动化测试框架编写完成,运行 start.py 程序,收到邮件内容如下:
标签:dict star exce edr 列表 from mode cto import
原文地址:https://www.cnblogs.com/pengzhuopeng/p/9118845.html