[英]Can conftest.py _read_ an existing value from pytest.ini?
conftest.py can be used to define properties in pytest.ini with parser.addini()
but can conftest.py also read existing values from pytest.ini? conftest.py 可用于使用
parser.addini()
定义 pytest.ini 中的属性,但 conftest.py 还可以从 pytest.ini 中读取现有值吗?
I would like to look at the value of a parameter in pytest.ini and use it to set the default value of another parameter in pytest.ini我想查看 pytest.ini 中一个参数的值,并用它来设置 pytest.ini 中另一个参数的默认值
Is this possible without having to use configparser myself in conftest.py?这是否可能无需在 conftest.py 中自己使用 configparser?
I would like to look at the value of a parameter in pytest.ini and use it to set the default value of another parameter in pytest.ini
我想查看 pytest.ini 中一个参数的值,并用它来设置 pytest.ini 中另一个参数的默认值
Best is to add a custom impl of the pytest_configure
hook.最好是添加
pytest_configure
钩子的自定义 impl。 Example: assume you have two custom ini options defined in your conftest.py
:示例:假设您在
conftest.py
中定义了两个自定义 ini 选项:
def pytest_addoption(parser):
parser.addini("fizz", help="help for my key", default="buzz")
parser.addini("spam", help="help for my key", default="eggs")
Now spam
should be set to bacon
whenever fizz
is not buzz
(not the default value).现在
spam
应该设置为bacon
,只要fizz
不是buzz
(不是默认值)。 Extend conftest.py
with:扩展
conftest.py
:
def pytest_configure(config):
fizz = config.getini("fizz")
spam = config.getini("spam")
print("values parsed from ini: fizz:", fizz, "spam:", spam)
if not fizz == "buzz":
# override parsed ini value
config._inicache["spam"] = "bacon"
print("spam was replaced to:", config.getini("spam"))
When running pytest -s
(and fizz
set to something else than buzz
in pytest.ini
), you will get the following output:运行
pytest -s
时(并且在pytest.ini
中设置的fizz
声不是buzz
),您将获得以下 output:
values parsed from ini: fizz: fuzz spam: eggs
spam was replaced to: bacon
============================= test session starts =============================
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.