pytest pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace'报错处理办法

一、背景说明

上周测试的同事说pytest+allure环境在别人电脑上跑没问题,在她的环境跑却报错让帮看一下。其实pytest只是听说过allure直接没听过,但不能直接表示不会,祭出百度大法。

看环境pytest直接以python库的形式安装就可以了:pip install pytest pytest-allure-adaptor pytest-rerunfailures pytest-html

allure安装也简单下载解压然后将bin目录添加到环境变量即可。下载地址:https://github.com/allure-framework/allure2/releases/tag/2.10.0

二、问题处理

2.1 报错描述

主要运行报错如下:

pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin' from 'd:\language\miniconda3\e
nvs\pytest\lib\site-packages\allure\pytest_plugin.py'>

完整报错如下:

F:PycharmProjectspytest>pytest
==================================================================== test session starts =====================================================================
platform win32 -- Python 3.6.6, pytest-4.3.0, py-1.7.0, pluggy-0.8.1
rootdir: F:PycharmProjectspytest, inifile: pytest.ini
plugins: rerunfailures-6.0, metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collecting 1 item                                                                                                                                             I
NTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packages\_pytestmain.py", line 210, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packages\_pytestmain.py", line 249, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packagespluggyhooks.py", line 284, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packagespluggymanager.py", line 68, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packagespluggymanager.py", line 62, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packagespluggycallers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packagespluggycallers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packagespluggycallers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packages\_pytestmain.py", line 259, in pytest_collection
INTERNALERROR>     return session.perform_collect()
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packages\_pytestmain.py", line 487, in perform_collect
INTERNALERROR>     self.config.pluginmanager.check_pending()
INTERNALERROR>   File "d:languageminiconda3envspytestlibsite-packagespluggymanager.py", line 251, in check_pending
INTERNALERROR>     % (name, hookimpl.plugin),
INTERNALERROR> pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin' from 'd:\language\miniconda3\e
nvs\pytest\lib\site-packages\allure\pytest_plugin.py'>
View Code

2.2 报错处理

反复重新创建配置了环境突然某次成功运行了,回头排除项目使用中文路径、python环境使用中文路径、使用conda发行版后,想起google到的某个页面说过是版本问题而且自己也按其建议装了旧版本:

pytest pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace'报错处理办法

最后确定确实如其所说,pytest新版本(我当前是4.3.0)运行报错,换成4.0.2版本就不会报错:

# 卸载已安装的pytest
pip uninstall pytest
# 安装4.0.2版本pytest
pip install pytest==4.0.2

成功运行如下图:

pytest pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace'报错处理办法

参考:

https://github.com/nucypher/nucypher/issues/649