From 2ab4bf13ab54cd02b162709be1777309d9171d0f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 26 Jul 2015 14:39:13 +0200 Subject: [PATCH] Document and test stacking of parametrize. Closes #815. --- doc/en/parametrize.txt | 12 ++++++++++++ testing/python/collect.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/doc/en/parametrize.txt b/doc/en/parametrize.txt index 35f93f3dd6b..05f18b23ba8 100644 --- a/doc/en/parametrize.txt +++ b/doc/en/parametrize.txt @@ -114,6 +114,18 @@ Let's run this:: The one parameter set which caused a failure previously now shows up as an "xfailed (expected to fail)" test. +To get all combinations of multiple parametrized arguments you can stack +``parametrize`` decorators:: + + import pytest + @pytest.mark.parametrize("x", [0, 1]) + @pytest.mark.parametrize("y", [2, 3]) + def test_foo(x, y): + pass + +This will run the test with the arguments set to x=0/y=2, x=0/y=3, x=1/y=2 and +x=1/y=3. + .. note:: In versions prior to 2.4 one needed to specify the argument diff --git a/testing/python/collect.py b/testing/python/collect.py index 5bbff6a64fe..97250028a84 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -472,6 +472,20 @@ def pytest_pyfunc_call(self, pyfuncitem): config.pluginmanager.register(MyPlugin2()) config.hook.pytest_pyfunc_call(pyfuncitem=item) + def test_multiple_parametrize(self, testdir): + modcol = testdir.getmodulecol(""" + import pytest + @pytest.mark.parametrize('x', [0, 1]) + @pytest.mark.parametrize('y', [2, 3]) + def test1(x, y): + pass + """) + colitems = modcol.collect() + assert colitems[0].name == 'test1[2-0]' + assert colitems[1].name == 'test1[2-1]' + assert colitems[2].name == 'test1[3-0]' + assert colitems[3].name == 'test1[3-1]' + def test_issue751_multiple_parametrize_with_ids(self, testdir): modcol = testdir.getmodulecol(""" import pytest