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

How to read custom config from pytest.ini?

我想在 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

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

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

您需要使用 pytest_addoption 钩子来使选项已知:

def pytest_addoption(parser):
    parser.addini('custom_value', 'documentation of my custom value')