Pytest 无法识别添加的选项

Pytest not recognizing added options

我的项目目录如下所示

Projects/
....this_project/
........this_project/
............__init__.py
............code.py
............tests/
................conftest.py
................test_1.py
................test_2.py

我通过将以下代码放入 conftest.py

添加了一个命令行选项 (--PALLADIUM_CONFIG)
def pytest_addoption(parser):
    parser.addoption("--PALLADIUM_CONFIG", action="store")

@pytest.fixture
def PALLADIUM_CONFIG(request):
    return request.config.getoption("--PALLADIUM_CONFIG")

奇怪的是:

如果我进入

Projects/this_project/this_project

Projects/this_project/this_project/tests

和运行

py.test --PALLADIUM_CONFIG=***

如果运行还好

但是如果我将自己定位在

Projects/this_project

Projects

然后 pytest 给我错误

py.test: error: unrecognized arguments: --PALLADIUM_CONFIG=***

这是 pytest 本身的限制。看看 docs:

Note that pytest does not find conftest.py files in deeper nested sub directories at tool startup. It is usually a good idea to keep your conftest.py file in the top level test or project root directory.

一个解决方案是创建一个外部插件,或者将选项移动到靠近根目录的 conftest 文件中。