Skip to main content
 首页 » 编程设计

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

2024年10月24日7wayfarer

我想在 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 Hook 来使选项已知:

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