python - Pytest 无法识别添加的选项

标签 python pytest

我有一个项目目录如下所示

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 文件中。

关于python - Pytest 无法识别添加的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522783/

相关文章:

python - 计算候选人被投票的次数

python - 在 Django 的 ModelChoiceField 中访问对象

python - 类型错误 : 'str' object is not callable when insert tweet data to mysql Python 3

python - Cartopy 使用正交投影错误地绘制点

python - 使用 Django 测试工作获取 live_server 固定装置

python - 重写代码使其在 Python 3.4 中工作时出现 AttributeError

python - Django py.test 找不到设置模块

python - 如何通过命令行将 firefox/chrome headless 模式传递给 pytest

Django 或 Django Rest Framework 在测试时无法解析 url 参数

python-3.x - Pytest 创建临时 CSV 文件以供读取