[TIP] Fwd: permutated parameters without nested decorators?

dpb dpb dpb.mediamath at gmail.com
Sun Mar 22 19:02:20 PDT 2015


Ah, sorry — neglected to reply to list.- dpb

---------- Forwarded message ----------
Date: Sun, Mar 22, 2015 at 10:01 PM

Thanks. I think it can be made considerably more readable as

import itertools
> parameters = itertools.product(range(1,5),
>             [i**3 for i in range(1,4)],
>             [i**5 for i in range(1,3)])
>
> @pytest.mark.parametrize('a, b, c', list(parameters))
> def test_funcs(a, b, c):
>     assert (a + b) ** 2 != c ** 2


(Py3K.) It's interesting that because of the way Metafunc.parametrize is
written (I'm looking at
https://bitbucket.org/pytest-dev/pytest/src/39361b33346e784e4f6359dbdba9c9957a8e4cd7/_pytest/python.py,
lines 823-831, argvalues is fully expanded into a list before test
functions are run, so there seems to be no advantage in using a generator
for argvalues.

- dpb

On Sat, Mar 21, 2015 at 10:35 AM, holger krekel <holger at merlinux.eu> wrote:

> On Fri, Mar 20, 2015 at 20:38 -0400, dpb dpb wrote:
> > I like very much that by using multiple parametrization decorators I can
> > test the permutations of a number of argvalues sequences of different
> > lengths. Here is an example with argvalues sequences of cardinality 4, 3,
> > and 2, respectively:
> >
> > # params.py
> > >
> > import pytest
> > >
> > @pytest.mark.parametrize('a', [i for i in range(1, 5)])
> > > @pytest.mark.parametrize('b', [i ** 3 for i in range(1, 4)])
> > > @pytest.mark.parametrize('c', [i ** 5 for i in range(1,
> > > 3)])
> > > def test_funcs(a, b, c):
> > >     assert (a + b) ** 2 != c ** 2
> > >
> >
> > All 24 permutations run once:
> >
> > $ py.test params.py
> > > ============================= test session starts
> > > ==============================
> > > platform darwin -- Python 3.4.2 -- py-1.4.26 -- pytest-2.6.4
> > > collected 24 items
> > >
> > > params/param5.py ........................
> > >
> > > ========================== 24 passed in 0.03 seconds
> > > ===========================
> > > $
> > >
> >
> > But I wonder if there is a way to do this in a single expression, rather
> > than as nested decorators.
>
> Maybe something like:
>
>     @pytest.mark.parametrize("a,b,c", list(itertools.product(
>             range(1,5),
>             [i**3 for i in range(1,4)],
>             [i**5 for i in range(1,3)])))
>
> although i don't think it's much more readable.
>
> best,
> holger
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20150322/55dd2264/attachment.htm>


More information about the testing-in-python mailing list