Accept plugin configuration in pytest.ini #13078
-
Is it possible for a plugin writer to get its configuration from pytest.ini? For example: [pytest]
python_files =
test_*.py
testpaths =
tests
[inline-snapshot]
snapshot_dir = tests/snapshots Then in def pytest_configure(config):
my_config = config.getini("inline-snapshot")
Currently it seems like the only way is to declare CLI options, and then for the user to add these options to But I'm a noob regarding pytest plugins and their config so I might be missing a few things 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I suppose plugins could re-read the config file to load their own settings: def pytest_configure(config):
iniconfig = configparser.ConfigParser()
iniconfig.read(config.inifile)
my_config = iniconfig["inline-snapshot"] Would you recommend that, or is there a better way? |
Beta Was this translation helpful? Give feedback.
-
You will need to call That doesn't let you define an entirely new section, however. Reading the ini yourself is definitely something I wouldn't recommend, as you probably won't be compatible with pytest configuration in |
Beta Was this translation helpful? Give feedback.
You will need to call
Parser.addini()
inpytest_addoption
to declare your options, before you useConfig.getini()
.That doesn't let you define an entirely new section, however. Reading the ini yourself is definitely something I wouldn't recommend, as you probably won't be compatible with pytest configuration in
pyproject.toml
, and might have possible subtle syntax differences to pytest's ini parsing.