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 ) |
分类:
pytest
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡