接口测试基本框架搭建(common部分)(持续更新中)
项目基本结构
1.common:封装的各种处理的方法
2.conf:配置文件
3.data:用例数据
4.logs:日志文件
5.reports:测试报告
6.testcases:封装测试用例类
7.runner.py:启动文件
common-----------handle_path
处理项目路径,其他模块只需导入此模块对象
```python for name, func in list(cls.__dict__.items()): if hasattr(func, DATA_ATTR): for i, v in enumerate(getattr(func, DATA_ATTR)): test_name = mk_test_name(name, getattr(v, "__name__", v), i) # 源码 # test_data_docstring = _get_test_data_docstring(func, v) # 修改 #----start---- if v.get("title"): test_data_docstring =v["title"] else: test_data_docstring = _get_test_data_docstring(func, v) #----end------
common-----------myddt
对原生ddt进行一些修改,用于第三方测试报告展示用例描述,修改后可显示用例描述。
```python for name, func in list(cls.__dict__.items()): if hasattr(func, DATA_ATTR): for i, v in enumerate(getattr(func, DATA_ATTR)): test_name = mk_test_name(name, getattr(v, "__name__", v), i) # 源码 # test_data_docstring = _get_test_data_docstring(func, v) # 修改 #----start---- if v.get("title"): test_data_docstring =v["title"] else: test_data_docstring = _get_test_data_docstring(func, v) #----end------
common-----------handle_logs
设置日志文件的等级(配置文件),按天进行记录,设置输出格式。
import loggingfrom logging.handlers import TimedRotatingFileHandlerfrom common.handle_path import BASE_DIR,LOG_DIRimport osfrom common.handle_config import conffile_path = os.path.join(LOG_DIR, conf.get("logging","log_name"))def create_logger(): #创建日志收集器 log = logging.getLogger("jiaru") #设置收集器等级 log.setLevel(conf.get("logging","level")) #创建日志输入渠道 fh = TimedRotatingFileHandler(file_path, when='d', interval=1, backupCount=7, encoding="utf-8") #设施文件输出等级 fh.setLevel(conf.get("logging","fh_level")) #添加到收集器中 log.addHandler(fh) # 4、设置日志输出格式 formatter = "%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s" mate = logging.Formatter(formatter) fh.setFormatter(mate) return loglog = create_logger()
common-----------handle_excel
处理excel文件的读取以及写入
import openpyxlclass excel: def __init__(self,file_name,sheet_name): self.file_name = file_name self.sheet_name = sheet_name def open_excel(self): # 第一步:将excel文件加载到一个工作簿对象中 self.wb = openpyxl.load_workbook(self.file_name) # 第二步:选择文件中的表单 self.sh = self.wb[self.sheet_name] def read_data(self): """读取数据""" self.open_excel() #数据放到列表里面(以行区分) res = list(self.sh.rows) #遍历出第一行当作key title = [] for i in res[0]: title.append(i.value) cases_data = [] #遍历其他行当作value for o in res[1:]: data = [] for p in o: data.append(p.value) r = dict(zip(title,data)) cases_data.append(r) return cases_data def write_data(self, row, column, value): """写数据""" self.open_excel() self.sh.cell(row=row, column=column, value=value) self.wb.save(self.file_name)
commom-----------handle_config
处理配置文件
import osfrom configparser import ConfigParserfrom common.handle_path import CONF_DIRclass Config(ConfigParser): def __init__(self, filename, encoding='utf-8'): super().__init__() self.read(filename, encoding=encoding)# 创建一个配置文件解析器conf = Config(os.path.join(CONF_DIR, "config.ini"))