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的配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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   刚刚好1  阅读(967)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示