def is_login(conf): flag = True if conf: for tmp_name in conf: conf_path = os.path.join(path, 'test_case_config', tmp_name) conf = getConfig.GetConfig(conf_path).get_conf("HTTP") if not set_tmp_headers(conf): flag = False logger.error(f"登录失败: {conf}") break return flag
import logging import os from concurrent_log_handler import ConcurrentRotatingFileHandler import get_work_dir from my_util import getConfig path = get_work_dir.get_base_dir() log_path = os.path.join(path, 'log') config_file = os.path.join(path, 'config.yaml') conf = getConfig.GetConfig(config_file).get_conf("LOG") class Logger(object): def __init__(self, logger_name): self.logger = logging.getLogger(logger_name) logging.root.setLevel(logging.NOTSET) self.log_file_name = os.path.join(log_path, 'log.log') self.backup_count = 5 self.maxBytes = 1024 * 1024 * 10 self.console_output_level = conf.get('CONSOLE_OUT') self.file_output_level = conf.get('FILE_OUT') self.formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s') def get_logger(self): """在logger中添加日志句柄并返回,如果logger已有句柄,则直接返回""" if not self.logger.handlers: console_handler = logging.StreamHandler() console_handler.setFormatter(self.formatter) console_handler.setLevel(self.console_output_level) self.logger.addHandler(console_handler)
from my_util import getConfig, excelHelper, logHelper, requestHelper, setHeaders, skip_case import ddt import json import sys import os import get_work_dir # 日志实例,用于写日志 logger = logHelper.Logger(__name__).get_logger() path = get_work_dir.get_base_dir() # case_data,用于读取excel表格中的测试数据,两个参数:表格的相对路径和sheet名称,根据实际配置 case_data = excelHelper.ExcelHelper.get_excel_list( 'zsa_cloud\province_PC_case.xlsx', '农村家宴') # 读取配置文件,获取测试地址和端口,根据实际项目配置 config_life = os.path.join(path, 'test_case_config', 'province_PC.yaml') conf = getConfig.GetConfig(config_life) base_url = conf.get_conf("HTTP").get('SERVER') KEY = conf.get_conf("HTTP").get('KEY') # 微服务请求头增加了token和userId,用于获取这两个参数。需要指定测试项目的配置文件中的“KEY”,以读取指定请求头文件。不传参数则返回默认请求头。 tmp_headers = setHeaders.get_headers_info(KEY) # 测试用例涉及到关联接口,用户获取配置文件下的接口和参数等数据,根据实际配置 path_dict = conf.get_conf("VillageParty") @ddt.ddt class TestPCVillageParty(unittest.TestCase): @classmethod def setUpClass(cls) -> None: logger.info(f"{__name__}开始测试") @classmethod
import unittest import os from BeautifulReport import BeautifulReport import yaml import arrow from shutil import copyfile from my_util import emailHelper, htmlTemp, getConfig, setHeaders, logHelper import get_work_dir logger = logHelper.Logger(__name__).get_logger() path = get_work_dir.get_base_dir() # 读取需要用到的配置文件,返回数据对象.根据实际配置 sys_conf_file = os.path.join(path, 'config.yaml') sys_conf = getConfig.GetConfig(sys_conf_file) """通过sys_conf获取具体配置信息""" # 获取需要用于登录项目的配置文件信息 temp_headers_list = sys_conf.get_conf("TEMP_HEADERS_LIST") # 获取用于执行的测试用例 test_case_dir = os.path.join(path, 'test_case') # 测试用例脚本目录 case_list_file = os.path.join(path, 'case_list.yaml') # 测试用例执行文件目录 # 获取nginx,由于代理html测试报告 index_html = sys_conf.get_conf("NGINX").get('INDEX_HTML') report_url = sys_conf.get_conf("NGINX").get('REPORT_URL') # email_conf = getConfig.get_conf('EMAIL') is_send = sys_conf.get_conf("SEND_EMAIL") """html测试报告配置""" report_dir = os.path.join(path, 'result') # 测试报告输出目录 report_date = arrow.now().format("YYYYMMDD")