[pytest-dev] Help with parametrized fixtures
Carl Meyer
carl at oddbird.net
Wed Nov 26 22:31:03 CET 2014
Hi Nikolaus,
On 11/26/2014 01:21 PM, Nikolaus Rath wrote:
> I have a parametrized fixture foo, and some test cases that use it:
>
> @pytest.fixture(params=(1,2,3,4,5))
> def foo(request):
> # ...
> return foo_f
>
> def test_1(foo):
> # ...
>
> def test_2(foo):
> # ....
>
> Now, I'd like to add an additional test, but running it only makes sense
> for some of the fixture parameters. So far I've solved this by providing
> the parameter value as a fixture attribute and skipping "unsupported" values:
>
> @pytest.fixture(params=(1,2,3,4,5))
> def foo(request):
> # ...
> foo_f.param = request.param
> return foo_f
>
> def test_3(foo):
> if foo.param not in (1,4,5):
> raise pytest.skip("doesn't make sense")
>
>
> This works, but I don't like it very much because it means that the test
> suite can never be executed without skipping some tests. I'd rather
> reserve skipping for cases where the test could in principle be
> executed, but for some reason cannot work at the moment (e.g. because
> there's no internet, or a required utility is not installed).
>
> Is there a way to solve the above scenario without skipping tests?
>
> Ideally, I'd be able to do something like this:
>
> @only_for_param((1,4,5))
> def test_3(foo):
> ...
I think in order to do this you'll need to use the
`pytest_generate_tests` hook:
http://pytest.org/latest/parametrize.html#pytest-generate-tests
I might be wrong (haven't had to use pytest_generate_tests much), but I
don't think you'll be able to keep the simple parametrized fixture and
layer this on top; I don't think pytest_generate_tests can tweak the
fixture params per-test. You'll need to remove the fixture
parametrization and do it instead via pytest_generate_tests, using
indirect=True.
Carl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20141126/728b99c8/attachment.sig>
More information about the pytest-dev
mailing list