Pytest: Combining multiple @pytest.mark.parametrize lines

Created on 3 Jul 2015  路  6Comments  路  Source: pytest-dev/pytest

I sometimes write tests something like this:

@pytest.mark.parametrize('foo, bar', itertools.product(['a', 'b', 'c'], [1, 2, 3]))
def test_things(foo, bar):
    ...

to test all combinations of foo and bar. The itertools.product call sometimes gets a bit more complicated which makes this hard to follow.

IMHO, it'd be nicer to be able to write those tests like this:

@pytest.mark.parametrize('foo', ['a', 'b'. 'c'])
@pytest.mark.parametrize('bar', [1, 2, 3])
def test_things(foo, bar):
    ...

And pytest would then generate all possible combinations. What are your thoughts on this?

I'm not sure if the difference between using two args in one parametrize call vs. using two parametrize calls with one argument each is too subtle, though.

parametrize docs proposal

Most helpful comment

This currently works for me (tested in pytest-2.7.0):

import pytest

@pytest.mark.parametrize('foo', ['a', 'b', 'c'])
@pytest.mark.parametrize('bar', [1, 2, 3])
def test_things(foo, bar):
    assert foo in ['a', 'b', 'c']
    assert bar in [1, 2, 3]
test_foo.py::test_things[1-a] PASSED
test_foo.py::test_things[1-b] PASSED
test_foo.py::test_things[1-c] PASSED
test_foo.py::test_things[2-a] PASSED
test_foo.py::test_things[2-b] PASSED
test_foo.py::test_things[2-c] PASSED
test_foo.py::test_things[3-a] PASSED
test_foo.py::test_things[3-b] PASSED
test_foo.py::test_things[3-c] PASSED

Did you have something else in mind?
_(Actually I also didn't know this worked hehehe)_

All 6 comments

This currently works for me (tested in pytest-2.7.0):

import pytest

@pytest.mark.parametrize('foo', ['a', 'b', 'c'])
@pytest.mark.parametrize('bar', [1, 2, 3])
def test_things(foo, bar):
    assert foo in ['a', 'b', 'c']
    assert bar in [1, 2, 3]
test_foo.py::test_things[1-a] PASSED
test_foo.py::test_things[1-b] PASSED
test_foo.py::test_things[1-c] PASSED
test_foo.py::test_things[2-a] PASSED
test_foo.py::test_things[2-b] PASSED
test_foo.py::test_things[2-c] PASSED
test_foo.py::test_things[3-a] PASSED
test_foo.py::test_things[3-b] PASSED
test_foo.py::test_things[3-c] PASSED

Did you have something else in mind?
_(Actually I also didn't know this worked hehehe)_

Oh wow, that works indeed! I didn't expect that :smile:

I wonder if it's intended or by accident - now the question is if it should be pointed out in the docs, and if there are tests for it.

Not sure if it's intended, but a PR for the docs would be welcome. :smile:

Can we close this then?

I want to contribute a PR for docs (and tests, if there aren't any yet) before closing this - though I probably won't have time for that until (or after?) EuroPython.

No problem! :smile:

This currently works for me (tested in pytest-2.7.0):

import pytest

@pytest.mark.parametrize('foo', ['a', 'b', 'c'])
@pytest.mark.parametrize('bar', [1, 2, 3])
def test_things(foo, bar):
    assert foo in ['a', 'b', 'c']
    assert bar in [1, 2, 3]
test_foo.py::test_things[1-a] PASSED
test_foo.py::test_things[1-b] PASSED
test_foo.py::test_things[1-c] PASSED
test_foo.py::test_things[2-a] PASSED
test_foo.py::test_things[2-b] PASSED
test_foo.py::test_things[2-c] PASSED
test_foo.py::test_things[3-a] PASSED
test_foo.py::test_things[3-b] PASSED
test_foo.py::test_things[3-c] PASSED

Did you have something else in mind?
_(Actually I also didn't know this worked hehehe)_

Thank you.

Was this page helpful?
0 / 5 - 0 ratings