Pytest: Someone can suggest the best way to handle external configuration in a big test suite?

Created on 31 Dec 2019  路  5Comments  路  Source: pytest-dev/pytest

i'm handling a very big test suite,
that needs references to a lot of assets, usernames, password, endpoints, and so on.
now,
all the configuration is stored inside a configuration.py module, imported from the test files.
when i need to change environment (e.g. from quality to production) i rename the configuration.py file using the right one.
Obviously this is not a good approach to configuration, but I'm having difficulties choosing the right approach.

my actual approach is to store configuration in a external json file and choose the file with a command line parameter, using pytest_addoption hook in conftest.py in order to get the configuration json as a dict in a fixture.

this approach has some problems probably because I don't know how to use pytest properly, so I apologize and ask for some advice :

  • if a take a procedural style, (test outside classes) every test needs to load the same fixture with parametrization and this is time consuming and not dry, due i have more than 400 tests.
  • if i take a OO style (test inside classes) i can load configuration once using a autoUse fixture at the beginning, defining properties of the class, but these properties cannot be used in @parametrize because self is not defined
  • if i take a OO style (test inside classes) i cannot load configuration once using autoUse fixture if tests use other fixtures because direct fixture are called before others (the one with the autoUse) as described here
  • i cannot use fixture values as parametrization argvalues, so i need to use pytest_generate_tests hook that parametrize fixture based on it name loading values from a plain function as described here and this , for me, is not dry
  • i cannot access pytest information outside pytest context, so i cannot create plain function that require this information

so i think i keep storing configuration in a configuration.py modules changing the configuration.py file with a script, in this way :

  • test can be parametrized reading arrays from a modules
  • i don't need to pass and fixture to test
  • i can use OO style
  • i don't need to use pytest_generate_tests

even i was searching for a more cute way to load configuration.

thanks for your time.

question

All 5 comments

i have a similar situation at a work project, we have a set of project local pytest plugins that facilitate providing the loaded configuration from convention based locations as well as taking cli options to puck the variant/environment for each test

those "local" pytest plugins get activated using a entrypoint and a lot of pytest_plugins declarations in other plugins (we don't just handle configuration loading but also markers, contextual parametrization, end2end environment selection and a few other details)

@RonnyPfannschmidt thanks for your suggestion. i will take a look at the plugin. i also tried this plugin https://github.com/wojole/pytest-testconfig but i think conftest runs before the plugin so if you need configuration in conftest you cannot use it (https://github.com/wojole/pytest-testconfig/issues/8)

@andreabisello declaring a own plugin using a entrypoint means its running before conftest

Closing this issue as I think the question has been answered.

yes, thanks.

Was this page helpful?
0 / 5 - 0 ratings