自己定义的代码块是在以下目录下: ~/Library/Developer/Xcode/UserData/CodeSnippets/ 感兴趣的同学可以自己去研究一下
import os
import pytest
from service.init_api import ROOT_DIR, COMMON
from service.send_email import get_content, mailSender
from service.utils import init_logger, traverse_file
from service.runner import BCPRunner
# init_logger()
RUN = BCPRunner()
def del_xml_file(xml_report_path):
# 看趋势,存储10次左右的执行结果,然后再清理xml_report文件夹
if not os.path.exists(xml_report_path):
os.mkdir(xml_report_path)
xml_fileList = traverse_file(xml_report_path)
file_count = len(xml_fileList)
print(f"当前有【 {file_count} 】个allure报告文件!!!")
if file_count > 2:
for path in xml_fileList:
if os.path.exists(xml_report_path + "/" + path):
os.remove(xml_report_path + "/" + path)
print("XML删除完成!!")
def allure_generate(xml_path, html_path):
try:
if not os.path.exists(html_path):
os.mkdir(html_path)
os.system('allure generate {path_allure} -o {path_html} --clean'
.format(path_allure=xml_path, path_html=html_path))
except Exception as e:
print(e)
def allure_test(testcase_path, xml_path, mark=None):
if not os.path.exists(xml_path):
os.mkdir(xml_path)
# 定义测试集
args = ['--alluredir={path_allure}'.format(path_allure=xml_path)]
# 标签
if not mark:
mark = COMMON['MARK']
try:
if testcase_path is not None:
test_args = ['-q', testcase_path, '-m', mark]
pytest.main(test_args + args)
else:
print("用户未选择执行项目,不执行")
except Exception as e:
print("Exception", str(e))
def pytest_html_test(testcase_path):
# 定义测试集
args = ['-q', '--html=./report/report.html', '--self-contained-html']
# 标签
try:
if testcase_path is not None:
test_args = [testcase_path]
pytest.main(test_args + args)
else:
print("用户未选择执行项目,退出!")
except Exception as e:
print("Exception", str(e))
def send_content():
content = get_content()
mail_detail = {"result": content, "env": COMMON["HOST"]}
sender = mailSender(mail_detail, [])
sender.send()
def run():
testcase_path = ROOT_DIR + "/testcases"
xml_path = ROOT_DIR + "/report/xml"
html_path = ROOT_DIR + "/report/html"
del_xml_file(xml_path)
allure_test(testcase_path, xml_path)
allure_generate(xml_path, html_path)
send_content()
# pytest_html_test(testcase_path)