def __init__(self, encoding='utf-8'): """初始化""" formatter = logging.Formatter() now_time = time.strftime('%Y-%m-%d') dir_log = '{}.log'.format(now_time) log_dir = read_file('log', dir_log) level = MyConfig('level').config logs_day = MyConfig('logs_save').config file_handler = logging.FileHandler(log_dir, encoding=encoding) file_handler.setFormatter(formatter) console_handler = logging.StreamHandler(sys.stdout) console_handler.formatter = formatter self.Logging = logging.getLogger(__name__) self.Logging.addHandler(file_handler) self.Logging.setLevel(level) self.current_time = standard_time() if isinstance(logs_day, int): for i in range(logs_day): dir_log = '{}.log'.format( (datetime.today() - timedelta(days=i + logs_day)).strftime('%Y-%m-%d')) log_dir = read_file('log', dir_log) exists = os.path.exists(log_dir) if exists: os.remove(log_dir) else: TypeError('日志最长保存天数格式错误,它需要是一个int型')
def __init__(self, module=None): """初始化""" self.skip_module = MyConfig('skip_module').config if current_module is None: self.current_module = _current_module() else: self.current_module = module
def __init__(self, case_data, browser_switch=True, html_name='html_report', encoding='utf-8'): """该方法主要用于,邮件发送的一些支持类方法""" self.html_name = html_name self.encoding = encoding self.switch_browser = browser_switch self.title = MyConfig('project_name').excel_parameter self.science = MyConfig('science').excel_parameter MyThread({self._browser_get_html: (case_data, )}).run()
def __init__(self, switch=False): """ 数据库(用于db) :param switch: """ self.sql = self._connect_sql() self.dbTable = MyConfig('sql_table').sql self.dbTitle = MyConfig('sql_create_title').sql self.dbQuery = MyConfig('sql_query').sql self.dbDelete = MyConfig('sql_delete').sql self.dbInsert = MyConfig('sql_insert').sql if switch: self._insert_title()
def get_log(): """获取项目logo""" url = MyConfig('url').base_url + MyConfig('logo_url').config r = requests.get(url, stream=True) if r.content: log_path = read_file('img', 'logo.png') with open(log_path, 'wb') as f: f.write(r.content) if os.path.exists(log_path): img = Image.open(log_path) x, y = img.size p = Image.new('RGBA', img.size, (255, 255, 255)) p.paste(img, (0, 0, x, y)) p.save(log_path)
def _get_case_status(self): """获取需要执行的路径,并执行用例""" module_run = MyConfig('module_run').config project_name = MyConfig('project_name').excel_parameter if module_run is not None: self.current_path = self.current_path + '/{}/{}'.format( project_name, module_run) discover = unittest.defaultTestLoader.discover(self.current_path, self.re) if self.thread: ConversionDiscover(discover).case_package() else: runner = unittest.TextTestRunner(verbosity=2).run(discover) DataHandleConversion().case_data_handle(in_case_data=runner) self._get_case_detailed()
def __init__(self, dirName='/BrowserToken.ini', encoding='utf-8'): """初始化""" self.path = os.path.realpath(os.path.dirname( os.path.dirname(__file__))) + dirName self.encoding = encoding self.config = configparser.ConfigParser() self.keys = MyConfig('token_keys').config
class Skip(object): def __init__(self, module=None): """初始化""" self.skip_module = MyConfig('skip_module').config if current_module is None: self.current_module = _current_module() else: self.current_module = module @property def _is_skip(self): """通过.ya中的数据对比当前模块名称是否相等""" if isinstance(self.skip_module, dict): for module, reason in self.skip_module.items(): if module == self.current_module: return True, reason else: raise TypeError @property def is_skip(self): """用例执行""" skip = self._is_skip if skip: return skip[0] @property def is_reason(self): """跳过原因""" reason = self._is_skip if reason: return reason[1]
def __init__(self, first, second, id, level, name, remark, status, reason, url, time, driver, module, screenshots_path, author, myself, error_path, log=None, encoding='utf8'): """初始化""" self.first = first self.second = second self.id = id self.level = level self.name = name self.remark = remark self.status = status self.reason = reason self.url = url self.time = time self.driver = driver self.screenshots_path = screenshots_path self.author = author self.myself = myself self.log = log self.error_path = error_path self.module = module self.encoding = encoding self.img_path = None self.thread = MyConfig('thread').config self.sql_type = MyConfig('execute_type').sql self.project = MyConfig('project_name').excel_parameter self.case_path = read_file(self.project, 'case.txt') if 'my_sql' == self.sql_type: self.sql = Mysql() else: self.sql = MyDB()
def setUpClass(cls): """判断类下面是否需要重新请求账号登录""" cls.driver = browser(switch=MyConfig('browser').config) cls.wait = MyConfig('page_loading_wait').config cls.sql = MyDB() cls.log = Logger() if cls.RE_LOGIN: account = cls.LOGIN_INFO['account'] password = cls.LOGIN_INFO['password'] company = cls.LOGIN_INFO['company'] if account and password: cls.login = LoginPublic( driver=cls.driver, account=account, password=password, company=company, module=cls.MODULE.split('\\')[-1].split('.')[0]) cls.login.login() else: raise LoginSelectError( cls.MODULE.split('\\')[-1].split('.')[0])
def __init__(self): """初始化""" self.log = Logger() self.time = standard_time() self.path = os.path.realpath(__file__) self.file_path = MyConfig('project_name').excel_parameter self.all_param = MyProject(self.file_path).parameter_ui self.paths = self.file_path self.init = '__init__.py' self.currency_py = 'currency.py' self.currency_ya = 'currency.yaml' self.common_ya = 'common.yaml' self.common_py = 'common.py' self.encoding = 'utf-8'
def __init__(self): self.Mail = smtplib.SMTP() self.contents = MIMEMultipart() self.sender = MyConfig('send_account').send_email self.sender_password = MyConfig('send_password').send_email self.server = MyConfig('server').send_email self.receiver = MyConfig('receiver').send_email self.title_name = MyConfig('project_name').excel_parameter self.title = MyConfig('science').excel_parameter self.img_path = read_file('img', 'html.png') self.excel_path = read_file('report', 'ExcelReport.xlsx')
def __init__(self, module="about", class_name="TestAboutUs", case_name="test_profile"): """ 初始化,读取common中的数据,self.data_messages为对应数据 :param module: 模块:如 staff_manage :param class_name: 类:如:'className': 'TestLogin' :param case_name: 用例名称:如:test_accountError """ try: global url, value self.module = module self.class_name = class_name self.case_name = case_name self.url = MyConfig('url').base_url self.all_parm = MyProject('').parameter_ui data_messages = {} for a in self.all_parm[module]: if a["className"] == class_name: if a['url'] is None: url = self.url else: url = self.url + a['url'] for b in a["funName"]: try: value = b[case_name] if value["url"] is not None: url = self.url + value["url"] data_messages["url"] = url data_messages["author"] = value["author"] data_messages["level"] = value["level"] data_messages["asserts"] = value["asserts"] data_messages["scene"] = value["scene"] except Exception as exc: reason = "{}.{}.{}.common.yaml中的caseName与测试类caseName不存在,该条用例已终止测试...原因:{}".\ format(self.module, self.class_name, self.case_name, exc) warnings.warn(reason) if data_messages: self.data_messages = data_messages else: reason_one = "{}.{}.{}.data_messages无数据,请检查对应参数是否正确,该条用例已终止测试...".\ format(self.module, self.class_name, self.case_name) warnings.warn(reason_one) except Exception as exc: warnings.warn('存在异常错误' + str(exc))
def __init__(self, encoding='utf8'): """初始化""" self.current_path = os.path.dirname(__file__) self.re = MyConfig('re').config sql_type = MyConfig('execute_type').sql if 'my_sql' == sql_type: self.sql = Mysql() else: self.sql = MyDB() project = MyConfig('project_name').excel_parameter path = read_file(project, 'case.txt') with open(path, 'wt', encoding=encoding): pass self.mail = Email() self.start_time = standard_time() self.wait = MyConfig('while_sleep').config self.case = MyConfig('while_case').config self.thread = MyConfig('thread').config self.excel = ExcelTitle self._clear_sql()
def __init__(self, switch=False, coding='utf8'): """ 初始化数据库数据(用于mysql) :param switch: False:不创建表字段,True创建表 :param coding: 转码 """ self.dbHost = MyConfig('address').sql self.dbUser = MyConfig('account').sql self.dbPsw = MyConfig('password').sql self.dbPort = MyConfig('port').sql self.dbBase = MyConfig('name_db').sql self.dbTable = MyConfig('sql_table').sql self.dbTitle = MyConfig('sql_create_title').sql self.dbQuery = MyConfig('sql_query').sql self.dbInsert = MyConfig('sql_insert').sql self.dbDelete = MyConfig('sql_delete').sql self.dbUpdate = MyConfig('sql_update').sql self.dbCreate = MyConfig('sql_create_list').sql self.decoding = coding self.DB = self._check_sql_list() if switch: self._insert_title()
def open_browser(self): """ 打开浏览器 :return: 返回新浏览器的session """ return browser(MyConfig('browser').config)
def __init__(self): """初始化""" path = os.path.dirname(__file__) self.runner = os.path.join(path, 'runner.py').replace('\\', '/') self.times = MyConfig('task_time').config