升级到 3.7.4 后替代 pytest 命名空间 - 错误:未知钩子'pytest_namespace'



我升级到最新的 3.7.4,我看到 pytest 命名空间已被弃用 https://docs.pytest.org/en/latest/deprecations.html#pytest-namespace

当我对模块运行pytest时,我出现错误:

pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace' in plugin <module 

我有一个小 conftest.py 文件,我可以在其中从数据库位置和类似位置解析出一些变量。然后我在pytest模块文件中使用这些变量(来自def pytest_namespace((函数(。但后来我得到了钩子错误测试。

是否有其他用途,这样我就不必重建大量代码?

提前谢谢。

线索是改变最新 pytest 版本中的思维方式。 根据更新日志pytest_namespace将折旧。所以现在你必须代替:

# conftest.py
def pytest_namespace():
    return dict(something_func: your_function, something_class: your_class)

用:

# conftest.py
def your_function():
    ...
class your_class:
    ...
def pytest_configure():
    pytest.something_func   = your_function
    pytest.something_class  = your_class()
遇到与

pytest_namespace相关的相同类型的问题。

  1. 从测试源代码的父目录运行测试套件(存在问题(
  2. 然后遍历到每个子目录并运行测试套件(问题仍然存在(

然后我决定安装任何可能定义了默认pytest_namespace的外部插件。

不知道这是否是一个实际的答案。但是我发现安装与pytest插件相关的外部插件解决了我的问题。

这可能是一个愚蠢的方法

遵循的步骤:

  1. 清除源代码目录中与 pytest 相关的旧残留文件 rm -rf .pytest_cache __pycache__
  2. 安装以下 pytest 软件包。

pip install pytest-assume==1.2
pip install pytest-cagoule==0.3.0
pip install pytest-cov==2.6.0
pip install pytest-csv==1.1.2
pip install pytest-expect==1.1.0
pip install pytest-html==1.19.0
pip install pytest-json==0.4.0
pip install pytest-metadata==1.7.0
pip install pytest-pdb==0.3.1
pip install pytest-pylint==0.14.0
pip install pytest-raises==0.9
pip install pytest-rerunfailures==6.0
pip install pytest-timeout==1.3.2
pip install pytest-xdist==1.8
pip install python-dateutil==2.7.3
成功安装上述插件后,不知何故,PluginValidationError: unknown hook 'pytest_namespace' in plugin错误后来没有出现。

目前正在研究它以找到实际原因。

最新更新