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

问题描述:

我有一个项目目录看起来像以下py.test:错误:无法识别参数

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") 

什么奇怪的是:

如果我cd到

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文件较近的根源。