代码块又称为初始化块,属于类中的成员[即是类的一部分],类似于方法(可以看作只有方法体的方法),讲逻辑语句封装在方法体 中,通过{ }包围起来。 但和方法不同,没有方法名,没有返回值,没有参数,只有方法体,而且不用通过对象或者类显式调用,而是加载类时,或创建对象时隐式调用。
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)