Selenium+unittest(2)配置模块介绍
配置文件config.yml如下
system_name: 后台管理系统(测试环境) url: http://test.com.cn/ cases_path: tests/ xml_path: xml/ yaml_path: yaml/ driver_type: chrome log: file_name: test.log backup: 10 console_level: INFO file_level: INFO pattern: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
配置读取config.py如下
""" 读取配置。这里配置文件用的yaml,也可用其他如XML,INI等。 """ import os from src.util.file_handler import YamlReader # 通过当前文件的绝对路径,其父级目录一定是框架的base目录,然后确定各层的绝对路径。如果你的结构不同,可自行修改。 BASE_PATH = os.path.split(os.path.split(os.path.dirname(os.path.abspath(__file__)))[0])[0] CONFIG_FILE = os.path.join(BASE_PATH, 'config', 'config.yml') DOWNLOAD_PATH = os.path.join(BASE_PATH, 'download') DRIVER_PATH = os.path.join(BASE_PATH, 'drivers') FILE_PATH = os.path.join(BASE_PATH, 'file') LOG_PATH = os.path.join(BASE_PATH, 'log') REPORT_PATH = os.path.join(BASE_PATH, 'report') REPORT_FILE = os.path.join(REPORT_PATH, 'report.html') SUFFIX = '.xml' LOGIN_SUCCESS = 'init' class Config: def __init__(self, config=CONFIG_FILE): self.config = YamlReader(config).data if not os.path.exists(LOG_PATH): os.makedirs(LOG_PATH) if not os.path.exists(REPORT_PATH): os.makedirs(REPORT_PATH) def get(self, element, index=0): """ yaml是可以通过'---'分节的。用YamlReader读取返回的是一个list,第一项是默认的节,如果有多个节,可以传入index来获取。 这样我们其实可以把框架相关的配置放在默认节,其他的关于项目的配置放在其他节中。可以在框架中实现多个项目的测试。 """ return self.config[index].get(element) class RunConfig: """ 运行测试配置 """ c = Config() # 测试系统名称 system_name = c.get('system_name') # 测试地址 url = c.get('url') # 日志配置 log = c.get('log') # 运行测试用例的目录 cases_path = os.path.join(BASE_PATH, c.get('cases_path')) # xml脚本目录 xml_path = os.path.join(BASE_PATH, c.get('xml_path')) # xml脚本目录 yaml_path = os.path.join(BASE_PATH, c.get('yaml_path')) # 配置浏览器驱动类型(chrome/firefox/ie) driver_type = c.get('driver_type') if __name__ == '__main__': print(RunConfig.url)