[TIP] How to parameterize a fixture based on a parser option with py.test?

holger krekel holger at merlinux.eu
Fri Dec 13 06:36:18 PST 2013

On Fri, Dec 13, 2013 at 15:06 +0100, Wouter Overmeire wrote:
> Somehow i missed the point that it is possible to access the config object
> trough the metafunc argument of pytest_generate_tests when i first looked
> at the docs. Based on your recommendations i gave it another go, problem
> solved. Thanks for your feedback!
> There is however another problem now. There is a big difference in the
> order of configuration / test execution when deferring the setup of
> parametrized resources.
> When using the params argument of the pytest.fixture decorator the tests
> are run such that for a given config of the platform, all tests run after
> each other. This is what i want, since the config is time-consuming
> compared to running the tests. When using the pytest_generate_tests
> function to parameterize the platform fixture, the tests are run, such that
> one give tests is executed for all configs, than the next test for all
> selcted configs, and so on. Is there an easy way to change the ordering? Or
> should i implement my own ordering, maybe using
> pytest_collection_modifyitems?

If configs are a per-session thing (it sounds so), you should additionally
pass "scope='session'" to metafunc.parametrize(), which should effect
the ordering you ask for. Other caching scope values are 'module' or 'class'
but most often people use 'session' and 'function' (the default).

Does that solve it?

More information about the testing-in-python mailing list