From c24ffa3b4c777d5ec009eb3fa2a63a3e593ab8d9 Mon Sep 17 00:00:00 2001 From: Leonard Lausen Date: Sun, 8 Oct 2017 12:23:26 +0900 Subject: [PATCH 1/4] Fix pytest.parametrize when argnames are specified as kwarg --- _pytest/fixtures.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index f57031e1ad0..8d5e0c4b31c 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -1037,9 +1037,13 @@ def pytest_generate_tests(self, metafunc): if faclist: fixturedef = faclist[-1] if fixturedef.params is not None: - func_params = getattr(getattr(metafunc.function, 'parametrize', None), 'args', [[None]]) + parametrize_func = getattr(metafunc.function, 'parametrize', None) + func_params = getattr(parametrize_func, 'args', [[None]]) # skip directly parametrized arguments - argnames = func_params[0] + if "argnames" in parametrize_func.kwargs: + argnames = parametrize_func.kwargs["argnames"] + else: + argnames = func_params[0] if not isinstance(argnames, (tuple, list)): argnames = [x.strip() for x in argnames.split(",") if x.strip()] if argname not in func_params and argname not in argnames: From 48b5c13f73f0bda248fd2d4f13ea054b0e7b31b9 Mon Sep 17 00:00:00 2001 From: Leonard Lausen Date: Sun, 8 Oct 2017 12:33:02 +0900 Subject: [PATCH 2/4] Add changelog for #2819 --- changelog/2819.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2819.bugfix diff --git a/changelog/2819.bugfix b/changelog/2819.bugfix new file mode 100644 index 00000000000..303903cf745 --- /dev/null +++ b/changelog/2819.bugfix @@ -0,0 +1 @@ +Fix issue with @pytest.parametrize if argnames was specified as kwarg. \ No newline at end of file From e89abe6a40747a8ba592ea722264d99f7a0ed5e0 Mon Sep 17 00:00:00 2001 From: Leonard Lausen Date: Mon, 9 Oct 2017 00:37:27 +0900 Subject: [PATCH 3/4] Defensive fallback in case of kwargs not being present --- _pytest/fixtures.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index 8d5e0c4b31c..e1c2d05f432 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -1039,8 +1039,9 @@ def pytest_generate_tests(self, metafunc): if fixturedef.params is not None: parametrize_func = getattr(metafunc.function, 'parametrize', None) func_params = getattr(parametrize_func, 'args', [[None]]) + func_kwargs = getattr(parametrize_func, 'kwargs', {}) # skip directly parametrized arguments - if "argnames" in parametrize_func.kwargs: + if "argnames" in func_kwargs: argnames = parametrize_func.kwargs["argnames"] else: argnames = func_params[0] From e86ba41a320f2dd4b28eb1f96eddf77069f46950 Mon Sep 17 00:00:00 2001 From: Leonard Lausen Date: Mon, 9 Oct 2017 01:02:54 +0900 Subject: [PATCH 4/4] Add testcase for #2819 --- testing/test_mark.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/testing/test_mark.py b/testing/test_mark.py index ae070f3a0f9..dc51bbac0f2 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -342,6 +342,24 @@ def test_func(foo, bar): ]) +def test_parametrized_with_kwargs(testdir): + """Test collect parametrized func with wrong number of args.""" + py_file = testdir.makepyfile(""" + import pytest + + @pytest.fixture(params=[1,2]) + def a(request): + return request.param + + @pytest.mark.parametrize(argnames='b', argvalues=[1, 2]) + def test_func(a, b): + pass + """) + + result = testdir.runpytest(py_file) + assert(result.ret == 0) + + class TestFunctional(object): def test_mark_per_function(self, testdir):