# -*- coding: utf-8 -*- import collections import importlib import os import sys import threading import openpyxl from data.wcar_product_data import product_data from data.product_data.wcar_case import WcarCaseDb from data.product_data.wcar_project import WcarProjectDb from data.product_data.wcar_case_info import WcarCaseInfoDb from func.wcar_log import printi, printe from func.wcar_wapper import wcar from func.wcar_config import C_SOFT_PATH from func.wcar_config import config class WcarCase(object): _instance_lock = threading.Lock() def __init__(self): self.__all_cases_list = list() self.__all_book_cases_list = list() self.__had_add_path = False self.__read_case_flag = 'loading' self.workbook = None @classmethod def instance(cls, *args, **kwargs): with WcarCase._instance_lock: if not hasattr(WcarCase, "_instance"): WcarCase._instance = WcarCase(*args, **kwargs) return WcarCase._instance @wcar() def fwk_write_py_case_info_to_db(self): product_data.fwk_db_clean_case_info() product_data.clear_fast_debugging_info() self.fwk_add_all_cases_path() #case_info.fwk_write_excel_case_info_to_db("云端", "文本用例", "book_case_scripts", False) for file_name in self.__all_cases_list: self.__write_case_info(file_name) # 用例状态初始化 WcarCaseDb().fwk_case_init() # 任务状态初始化 WcarProjectDb().fwk_project_init() self.__read_case_flag = 'done' return True def fwk_get_write_case_info_proccess(self): return self.__read_case_flag @wcar() def fwk_write_excel_case_info_to_db(self, car_type, case_type, script_name, flag): ret_dict = dict() self.__all_book_cases_list = [] temp_dict = collections.OrderedDict() self.fwk_add_all_cases_path() ret_dict['cases'] = [] WcarCaseInfoDb().del_book_case_info() sheet_list = [HV_CASE_SHEET, HV_ACT_SHEET, HV_ACT_COLL_SHEET, HV_JUDGE_SHEET] if not config.BOOK_CASE_PATH or not os.path.exists(config.BOOK_CASE_PATH): printe("Excel file not found!") return False try: excel_obj = BookCaseLoader(config.BOOK_CASE_PATH, sheet_list) all_case_info_list = excel_obj.get_all_case_info() for case_info_dict in all_case_info_list: case_info_dict['script_name'] = script_name case_info_dict['car_type'] = car_type case_info_dict['case_type'] = case_type product_data.fwk_db_write_case_info(case_info_dict) if flag: temp_dict = self.__schedule_group(case_info_dict, script_name, temp_dict) else: if case_info_dict.get('case_flag'): temp_dict = self.__schedule_group(case_info_dict, script_name, temp_dict) self.__book_list_add_cases(temp_dict) ret_dict['cases'] = self.__all_book_cases_list except Exception as e: printe(e) return ret_dict @wcar() def fwk_add_all_cases_path(self): if self.__had_add_path: return True scripts_path = os.path.join(C_SOFT_PATH, 'scripts') if not os.path.isdir(scripts_path): printi("Case path is wrong, pls check scripts path") return False sys.path.append(scripts_path) ret = self.__add_scripts_path(scripts_path) self.__had_add_path = True return ret def __schedule_group(self, case_info_dict, script_name, temp_dict): count = case_info_dict.get('case_count', 1) case_name = case_info_dict.get('case_ename') case_group = case_info_dict.get('case_group', None) if case_group: case_dict = dict() if not temp_dict.get(case_group): self.__book_list_add_cases(temp_dict) temp_dict[case_group] = dict() case_dict.update({case_name: {'count': count, 'script_name': script_name}}) temp_dict[case_group].update(case_dict) else: self.__book_list_add_cases(temp_dict) self.__all_book_cases_list.append({case_name: {'count': count, 'script_name': script_name}}) return temp_dict def fwk_write_book_case_excel(self, read_case_list): self.workbook = openpyxl.Workbook() for sheet_name in list(read_case_list.keys()): case_data = read_case_list.get(sheet_name) column_list = case_data[0].keys() worksheet = self.workbook.create_sheet(sheet_name) column = 1 case_data_len = len(case_data) for i in range(case_data_len): row = i + 2 for column_value in column_list: worksheet.cell(int(1), column, list(column_list)[column - 1]) worksheet.cell(int(row), column, case_data[i].get(column_value)) column = column + 1 column = 1 self.workbook.save(config.BOOK_CASE_PATH) def __book_list_add_cases(self, temp_dict): self.__all_book_cases_list.extend(temp_dict.values()) temp_dict.clear() def __add_scripts_path(self, spath): for efile in os.listdir(spath): fpath = os.path.join(spath, efile) if os.path.isdir(fpath): sys.path.append(fpath) self.__add_scripts_path(fpath) else: if efile == '__init__.py' or not (str(efile).endswith('.py') or str(efile).endswith('pyd')) or not str( efile).startswith('case_'): continue file_name = efile.split('.py')[0] self.__all_cases_list.append(file_name) return True def __write_case_info(self, file_name): def write_case_info(file_name, case_ename='', case_cname='', case_function='', case_environment=''): module_obj = self.__import_case_obj(file_name) if not module_obj: return False if not hasattr(module_obj, 'case_info_dict'): return False if hasattr(module_obj, 'case_info_dict'): case_info_dict = getattr(module_obj, 'case_info_dict') case_info_dict["case_count"] = 1 case_info_dict["case_flag"] = False case_info_dict["case_group"] = "" case_info_dict['case_cname'] = case_cname if case_cname else case_info_dict['case_cname'] case_info_dict["script_name"] = file_name case_info_dict['case_function'] = case_function if case_function else case_info_dict['case_function'] case_info_dict['case_environment'] = case_environment if case_environment else case_info_dict.get('case_environment', '') db_dict = {} db_dict.update(case_info_dict) db_dict['case_ename'] = case_ename if case_ename else file_name product_data.fwk_db_write_case_info(db_dict) module_obj = self.__import_case_obj(file_name) if not module_obj: return False if hasattr(module_obj, 'case_info_dict'): write_case_info(file_name) elif hasattr(module_obj, 'case_info_list'): case_info_list = getattr(module_obj, 'case_info_list') for case_info_dict in case_info_list: write_case_info(case_info_dict["script_name"], case_info_dict["case_ename"], case_info_dict["case_cname"], case_info_dict["case_function"], case_info_dict.get("case_environment")) else: return False return True @staticmethod def __import_case_obj(file_name): try: return importlib.import_module(file_name) except AttributeError as e: printe(file_name + ":" + str(e)) except ModuleNotFoundError as e: printe(file_name + ":" + str(e)) except ValueError as e: printe(file_name + ":" + str(e)) except ImportError as e: printe(file_name + ":" + str(e)) except TypeError as e: printe(file_name + ":" + str(e)) except Exception as e: printe(file_name + ":" + str(e)) return False case_info = WcarCase.instance()
按照命名规则加载用例
最新推荐文章于 2024-07-19 16:36:18 发布