2015-07-20 199 views
5

我有一個項目目錄看起來像以下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=*** 

回答

4

這是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文件較近的根源。