2015-07-21 32 views
0

我正在使用pytest在多個環境中運行測試,我想將這些信息(理想情況下)包含在ini樣式的配置文件中。我也想在命令行中覆蓋部分或全部配置。我試圖用鉤pytest_addoptionconftest.py像這樣:我可以在pytest套件中添加ini風格的配置嗎?

def pytest_addoption(parser): 
    parser.addoption("--hostname", action="store", help="The host") 
    parser.addoption("--port", action="store", help="The port") 

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

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

使用這個我可以添加在命令行配置信息,而不是在配置文件中。我也試過添加

[pytest] 
addopts = --hostname host --port 311 

我的pytest.ini文件,但沒有奏效。有沒有辦法做到這一點,而不建立我自己的插件?謝謝你的時間。

回答

1

解析器對象也有一個addini方法,您可以通過ini文件指定配置選項。 這裏是文檔吧:https://pytest.org/latest/writing_plugins.html?highlight=addini#_pytest.config.Parser.addini

addini(name, help, type=None, default=None)[source] 註冊一個ini文件選項。

Name: name of the ini-variable

Type: type of the variable, can be pathlist, args, linelist or bool.

Default: default value if no ini-file option exists but is queried.

INI-變量的值可以通過一個呼叫檢索到config.getini(名稱)。

相關問題