[TIP] permutated parameters without nested decorators?

holger krekel holger at merlinux.eu
Sat Mar 21 07:35:50 PDT 2015


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



More information about the testing-in-python mailing list