pytest运行触发AssertionError,疑与pluggy包相关
pytest 内部 AssertionError 问题(关联pluggy)
运行pytest时触发内部错误,抛出AssertionError,该问题疑似与pluggy包相关。异常发生在路径/home/test/project/env/lib/python3.8/site-packages/_pytest/terminal.py的第592行,具体为断言assert self._session失败。
完整报错栈信息如下:
INTERNALERROR> Traceback (most recent call last): INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/_pytest/main.py", line 270, in wrap_session INTERNALERROR> session.exitstatus = doit(config, session) or 0 INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/_pytest/main.py", line 324, in _main INTERNALERROR> config.hook.pytest_runtestloop(session=session) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__ INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result INTERNALERROR> raise ex[1].with_traceback(ex[2]) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/_pytest/main.py", line 349, in pytest_runtestloop INTERNALERROR> item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__ INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result INTERNALERROR> raise ex[1].with_traceback(ex[2]) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/_pytest/runner.py", line 113, in pytest_runtest_protocol INTERNALERROR> ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_hooks.py", line 265, in __call__ INTERNALERROR> return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_manager.py", line 80, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_callers.py", line 60, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_result.py", line 60, in get_result INTERNALERROR> raise ex[1].with_traceback(ex[2]) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/pluggy/_callers.py", line 39, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "/home/test/project/env/lib/python3.8/site-packages/_pytest/terminal.py", line 592, in pytest_runtest_logfinish INTERNALERROR> assert self._session INTERNALERROR> AssertionError
该断言逻辑对应pytest官方仓库中terminal.py的相关代码段,问题疑似关联pluggy项目。
可能的解决方案
- 版本兼容性检查:pytest与pluggy存在版本依赖关系,查看当前安装的pytest和pluggy版本,尝试升级或降级到官方推荐的匹配版本
- 重装依赖:删除虚拟环境中相关包,重新执行
pip install pytest,确保依赖完整且无损坏 - 排查自定义插件:如果项目使用了自定义pytest插件,检查插件是否在钩子函数中错误修改了session状态,导致
self._session被提前销毁 - 临时应急处理(不推荐):若需紧急运行测试,可临时注释掉terminal.py中的
assert self._session断言,但这只是权宜之计,需尽快定位根本原因
内容的提问来源于stack exchange,提问作者Lava Sangeetham
相关产品推荐
相关产品推荐

