[TIP] How to parameterize a fixture based on a parser option with py.test?
holger at merlinux.eu
Fri Dec 13 05:31:28 PST 2013
On Fri, Dec 13, 2013 at 12:21 +0100, Wouter Overmeire wrote:
> Using py.test (version 2.5.0) i would like to run a number of test on a
> user select-able platform with multiple configurations. A platform is a
> bunch measurement instruments, and hardware under test. The tests are not
> aware on which specific platform they run, nor how the platform is
> configured, they get a platform fixture object and do stuff with it.
> Using two parser arguments "--platform" and "--config"" the user can
> select a platform and a platform config. The platform fixture function gets
> the options from the request object, creates, configures and returns the
> platform. So far so good.
> However, test need to run for many configs (range 100) on a single
> platform, configs are grouped in sets. The user should be able to select
> the config set. Instead of selecting a single config i would like to select
> a set, the ""--config" option is now the name (string) referring to a
> factory function generating a list of configs. But i`m stuck on how to
> parameterize the platform fixture function using the "--config" parser
> option. The fixture decorator has a "parameters" argument to parameterize a
> fixture, which can do the trick for a hardcoded config set, but how to set
> the values based on a parser option? I also tried to use
> pytest.yield_fixture, but this supports only a single yield statement :-(.
I recommend you look into the pytest_generate_tests hook:
It allows to define actual parameters for building a fixture from
the command line. In the API description of Metafunc.parametrize()
on that page you find "indirect". If you set it to true then your
fixture function can access the indirect parameter via the typical
"request.param". See also the following example:
More information about the testing-in-python