# !/usr/bin/python # -*- coding: utf-8 -*- '''@Author:: Arthur Wu @Date:: 2024/10/8 ''' import os,pytest,subprocess,json,shutil,time,requests from configs.globalObj import * from configs.globalParams import * class Engine(): def __init__(self): self.CasesSuite = [] self.rootpath = os.path.dirname(os.path.dirname(__file__)) self.FolderPath = ReturnFolder self.projectCasesPath = str(os.path.join(GlobalPath['YMCaseScriptsPath']).replace("\\", "/")) self.ReportName = "Reports" def __execute(self): '''@Author:: Arthur Wu @Date:: 2024/5/9 :return: ''' for dirpath, dirnames, filenames in os.walk(self.projectCasesPath): for fn in filenames: if fn.startswith("test_"): caseScriptPath = os.path.join(dirpath, fn).replace("\\", "/") self.CasesSuite.append(caseScriptPath) if 0 == len(self.CasesSuite): raise ValueError("---[ERROR] the test suite was empty !") else: pytest.main(args=[*self.CasesSuite, '-vsq', '--alluredir', self.FolderPath]) subprocess.Popen( "allure generate " + self.FolderPath + ' -o ' + self.FolderPath + "html" + ' --workers=2 '+'--tests-per-worker=2', shell=True, stdout=subprocess.PIPE).stdout.read() LOGGER.info("---[INFO] Allure html report generate complete !") def __move_file(self): rootpath = (os.getcwd()).replace("\\", "/") pathli = rootpath.split("/") LastReportPath = '' PathList = self.FolderPath.split("/") try: LastReportPath = "D:/Program Files/AutoTestReports/" shutil.move(self.FolderPath, LastReportPath) except Exception as e: LOGGER.error(f"---Move the html report file error: {e}") return os.path.join(LastReportPath, PathList[-2]).replace("\\", "/") def __delete_pycache_dirs(self, target_dir): for root, dirs, files in os.walk(target_dir): for dir_name in dirs: if dir_name == '__pycache__': dir_path = os.path.join(root, dir_name) shutil.rmtree(dir_path) print(f"已删除: {dir_path}") def __delete_txt_files(self, target_dir): for root, dirs, files in os.walk(target_dir): for file_name in files: if file_name.endswith('.txt'): file_path = os.path.join(root, file_name) os.remove(file_path) print(f"已删除: {file_path}") def run_test_suite(self): txtPath = os.path.join(self.rootpath, "YiMao/ProcessData") self.__delete_pycache_dirs(self.rootpath) self.__delete_txt_files(txtPath) self.__execute() returnList = [] lastReportPath = self.__move_file() returnList.append(lastReportPath) self.__delete_txt_files(txtPath) self.__delete_pycache_dirs(self.rootpath) return returnList class NotificationModule(): def __init__(self): self.__headers = {'Content-Type': 'application/json;charset=utf-8'} datainfo = { "Debug": "7aedbee7239870e3e653748a2889d8bf063c61efa9213c7099bd57476066dc86", "Formal": "80b026022a28166cfc9eebaf8f6a880cc06f56a14b8803e8d67e7fb3cb05844e" } # self.urlInfo = f'https://oapi.dingtalk.com/robot/send?access_token={datainfo["Debug"]}' self.urlInfo = f'https://oapi.dingtalk.com/robot/send?access_token={datainfo["Formal"]}' self.ReportUrl = ProCfgData["ReportsURL"] self.ExecutionEnvironment = ProCfgData["ExecutionEnv"] def send_msg(self, ResultPath): ReportPath = os.path.join(ResultPath[0], 'html', 'index.html').replace('\\', '/') reportUrlpath = self.ReportUrl + ReportPath.split("AutoTestReports")[1] my_data = { "msgtype": "markdown", "markdown": { "title": "亿猫管理后台接口自动化测试", "text": f"#### 亿猫管理后台({self.ExecutionEnvironment})环境自动化测试执行结束: \n" f">#### [自动化测试报告链接(请点击查看)]({reportUrlpath})\n>\n" } } sess = requests.session() sess.keep_alive = False requests.post(url=self.urlInfo, data=json.dumps(my_data), headers=self.__headers, verify=False) sess.close()