122 lines
5.1 KiB
Python
122 lines
5.1 KiB
Python
# !/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.TestSuiteName = str(os.path.join(GlobalPath[ProCfgData["ExecutionScope"]]).replace("\\", "/"))
|
|
self.ReportName = "Reports"
|
|
self.newName = "亿猫业务场景接口自动化测试报告"
|
|
|
|
def __execute(self):
|
|
'''@Author:: Arthur Wu
|
|
@Date:: 2024/5/9
|
|
:return:
|
|
'''
|
|
for dirpath, dirnames, filenames in os.walk(self.TestSuiteName):
|
|
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()
|
|
folder_path = os.path.join(self.FolderPath, 'html').replace('\\', '/')
|
|
LOGGER.info("---[INFO] Allure html report generate complete !")
|
|
return folder_path
|
|
|
|
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 __set_overview_title(self, index_path, new_name):
|
|
title_filepath = os.path.join(index_path, "widgets", "summary.json")
|
|
with open(title_filepath, 'rb') as f:
|
|
params = json.load(f)
|
|
params['reportName'] = new_name
|
|
new_params = params
|
|
with open(title_filepath, 'w', encoding="utf-8") as f:
|
|
json.dump(new_params, f, ensure_ascii=False, indent=4)
|
|
|
|
def run_test_suite(self):
|
|
txtPath = os.path.join(self.rootpath, "YiMao/ProcessData")
|
|
self.__delete_pycache_dirs(self.rootpath)
|
|
self.__delete_txt_files(txtPath)
|
|
indexh5path = self.__execute()
|
|
self.__set_overview_title(indexh5path, self.newName)
|
|
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 = {
|
|
"Domain": "https://oapi.dingtalk.com/robot/send?access_token=",
|
|
"Debug": "7aedbee7239870e3e653748a2889d8bf063c61efa9213c7099bd57476066dc86",
|
|
"Formal": "80b026022a28166cfc9eebaf8f6a880cc06f56a14b8803e8d67e7fb3cb05844e"
|
|
}
|
|
self.urlInfo = datainfo["Domain"] + datainfo["Debug"]
|
|
# self.urlInfo = datainfo["Domain"] + 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()
|
|
|