pytest_terminal_summary重写收集测试报告并发送邮件,报错"Argument(s) {'Config'} are declared in the hookimpl but can not be found in the hookspec"

步骤:

1、conftest.py文件,重写pytest_terminal_summary(terminalreporter, exitstatus, config)

2、run执行pytest.main(),启动报错,提示填入的config,在hookspec找不到

错误提示如下:

pytest_terminal_summary  Argument(s) {'Config'} are declared in the hookimpl but can not be found in the hookspec

 

问题:当前版本的代码没有config的参数
1、导入_pytest.config一样启动失败

2、查看pytest关于这个方法的源码,查看到config参数是在pytest版本versionadded:: 4.2,而当前的pytest版本低于4.2

解决方法

1、升级pytest的版本

Pip install –upgrade pytest

2、 重启pycharm,在启动,启动成功,正确获取到config的配置信息

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    '''
    重写【pytest_terminal_summary】收集测试结果方法,将测试结果保存,传到发送邮件使用,作为主要内容,最后发送邮件
    :return:
    '''
    email = config.getoption('--sendemail')
    # email = True
    total = terminalreporter._numcollected
    pass_num = len(terminalreporter.stats.get("passed", []))
    failed_num = len(terminalreporter.stats.get("failed", []))
    error_num = len(terminalreporter.stats.get("error", []))
    skipped_num = len(terminalreporter.stats.get("skipped", []))
    param_t = {}
    param_t["total"] = total
    param_t["pass"] = pass_num
    param_t["failed"] = failed_num
    param_t["error"] = error_num
    param_t["skipped"] = skipped_num
    param_t["rate"] = (CommunFun().decimal_round(number=Decimal(pass_num/total), len='3'))*Decimal('100')
    # 将数量存放到com_obj,然后在发送邮件的时候,获取
    DataManage().add_data_obj({"testresult": param_t})
    if email:
        file = CommunFun().get_new_file(r"D:\xxxx\reports")
        sendEmail().send_email(email_to="xxx邮箱", filepath=file)

posted on 2022-05-20 09:47  刚刚好1  阅读(867)  评论(0编辑  收藏  举报

导航