searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

何以代码?

2024-06-13 12:45:26
3
0
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)
0条评论
0 / 1000
v****n
4文章数
0粉丝数
v****n
4 文章 | 0 粉丝
v****n
4文章数
0粉丝数
v****n
4 文章 | 0 粉丝
原创

何以代码?

2024-06-13 12:45:26
3
0
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)
文章来自个人专栏
天翼云数字化运营部测试
4 文章 | 3 订阅
0条评论
0 / 1000
请输入你的评论
0
0