如何从 pytest.ini 读取自定义配置?

pr4*_*4sh 5 python pytest

我想在 pytest.ini 文件上提供自定义参数并从代码中读取它。

[pytest]
markers =
    regression: mark a test as regression.
    sanity: mark a test as sanity.
    critical: mark a test as critical.
addopts= -sv --html=report.html
custom_value= test
Run Code Online (Sandbox Code Playgroud)

在这里,我想阅读 我在下面尝试过的custom_value但它不起作用并抛出ValueError: no option named 'custom_value'

def test_failtest(self, request):
    config = request.config
    tcv = config.getoption('custom_value')
    print "tcv->" + tcv
Run Code Online (Sandbox Code Playgroud)

The*_*ler 7

您需要使用pytest_addoption钩子来使该选项为人所知:

def pytest_addoption(parser):
    parser.addini('custom_value', 'documentation of my custom value')
Run Code Online (Sandbox Code Playgroud)