Python py.test:错误:无法识别的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31522783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:09:27  来源:igfitidea点击:

py.test: error: unrecognized arguments

pythonpytest

提问by Hello lad

I have a project directory looks like following

我有一个项目目录如下

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

and I added a command line option (--PALLADIUM_CONFIG) by putting following codes into conftest.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")

And what strange is:

奇怪的是:

if I cd into

如果我进入

Projects/this_project/this_project

or

或者

Projects/this_project/this_project/tests

and run

并运行

py.test --PALLADIUM_CONFIG=***

if runs well

如果运行良好

but if I locate myself in for example

但是如果我把自己定位在例如

Projects/this_project

or

或者

Projects

then pytest gives me error

然后pytest给我错误

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

采纳答案by Bruno Oliveira

That's a limitation of pytest itself. Take a look at the docs:

这是 pytest 本身的限制。看看文档

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.

请注意,pytest 在工具启动时不会在更深的嵌套子目录中找到 conftest.py 文件。将 conftest.py 文件保存在顶级测试或项目根目录中通常是个好主意。

One solution is to create an external plugin, or move the option to a conftestfile nearer the root.

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