Reputation: 31620
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?
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
Is this possible without having to use configparser myself in conftest.py?
Upvotes: 2
Views: 943
Reputation: 66441
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
Best is to add a custom impl of the pytest_configure
hook. Example: assume you have two custom ini options defined in your conftest.py
:
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). Extend conftest.py
with:
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:
values parsed from ini: fizz: fuzz spam: eggs
spam was replaced to: bacon
============================= test session starts =============================
...
Upvotes: 1