Skip to content

Commit fa1e088

Browse files
committed
Allow ovewriting a parametrized fixture while reusing the parent fixture's value
Fix #1953
1 parent 9c0e0c7 commit fa1e088

File tree

3 files changed

+140
-26
lines changed

3 files changed

+140
-26
lines changed

changelog/1953.bugfix.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Fix error when overwriting a parametrized fixture, while also reusing the super fixture value.
2+
3+
.. code-block:: python
4+
5+
# conftest.py
6+
import pytest
7+
8+
9+
@pytest.fixture(params=[1, 2])
10+
def foo(request):
11+
return request.param
12+
13+
14+
# test_foo.py
15+
import pytest
16+
17+
18+
@pytest.fixture
19+
def foo(foo):
20+
return foo * 2

src/_pytest/fixtures.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,34 +1529,36 @@ def sort_by_scope(arg_name: str) -> int:
15291529
return initialnames, fixturenames_closure, arg2fixturedefs
15301530

15311531
def pytest_generate_tests(self, metafunc: "Metafunc") -> None:
1532+
"""Generate new tests based on parametrized fixtures used by the given metafunc"""
15321533
for argname in metafunc.fixturenames:
1533-
faclist = metafunc._arg2fixturedefs.get(argname)
1534-
if faclist:
1535-
fixturedef = faclist[-1]
1534+
# Get the FixtureDefs for the argname.
1535+
fixture_defs = metafunc._arg2fixturedefs.get(argname)
1536+
if not fixture_defs:
1537+
# Will raise FixtureLookupError at setup time if not parametrized somewhere
1538+
# else (e.g @pytest.mark.parametrize)
1539+
continue
1540+
1541+
# In the common case we only look at the fixture def with the
1542+
# closest scope (last in the list). But if the fixture overrides
1543+
# another fixture, while requesting the super fixture, keep going
1544+
# in case the super fixture is parametrized (#1953).
1545+
for fixturedef in reversed(fixture_defs):
1546+
# Fixture is parametrized, apply it and stop.
15361547
if fixturedef.params is not None:
1537-
markers = list(metafunc.definition.iter_markers("parametrize"))
1538-
for parametrize_mark in markers:
1539-
if "argnames" in parametrize_mark.kwargs:
1540-
argnames = parametrize_mark.kwargs["argnames"]
1541-
else:
1542-
argnames = parametrize_mark.args[0]
1543-
1544-
if not isinstance(argnames, (tuple, list)):
1545-
argnames = [
1546-
x.strip() for x in argnames.split(",") if x.strip()
1547-
]
1548-
if argname in argnames:
1549-
break
1550-
else:
1551-
metafunc.parametrize(
1552-
argname,
1553-
fixturedef.params,
1554-
indirect=True,
1555-
scope=fixturedef.scope,
1556-
ids=fixturedef.ids,
1557-
)
1558-
else:
1559-
continue # Will raise FixtureLookupError at setup time.
1548+
metafunc.parametrize(
1549+
argname,
1550+
fixturedef.params,
1551+
indirect=True,
1552+
scope=fixturedef.scope,
1553+
ids=fixturedef.ids,
1554+
)
1555+
break
1556+
1557+
# Not requesting the overridden super fixture, stop.
1558+
if argname not in fixturedef.argnames:
1559+
break
1560+
1561+
continue # try super fixture, if any.
15601562

15611563
def pytest_collection_modifyitems(self, items: "List[nodes.Item]") -> None:
15621564
# Separate parametrized setups.

testing/python/fixtures.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,98 @@ def test_spam(spam):
396396
result = testdir.runpytest(testfile)
397397
result.stdout.fnmatch_lines(["*3 passed*"])
398398

399+
def test_override_fixture_reusing_super_fixture_parametrization(self, testdir):
400+
"""Override a fixture at a lower level, reusing the higher-level fixture that
401+
is parametrized (#1953).
402+
"""
403+
testdir.makeconftest(
404+
"""
405+
import pytest
406+
407+
@pytest.fixture(params=[1, 2])
408+
def foo(request):
409+
return request.param
410+
"""
411+
)
412+
testdir.makepyfile(
413+
"""
414+
import pytest
415+
416+
@pytest.fixture
417+
def foo(foo):
418+
return foo * 2
419+
420+
def test_spam(foo):
421+
assert foo in (2, 4)
422+
"""
423+
)
424+
result = testdir.runpytest()
425+
result.stdout.fnmatch_lines(["*2 passed*"])
426+
427+
def test_override_top_level_fixture_reusing_super_fixture_parametrization(
428+
self, testdir
429+
):
430+
"""Same as the above test, but with another level of overwriting."""
431+
testdir.makeconftest(
432+
"""
433+
import pytest
434+
435+
@pytest.fixture(params=['unused', 'unused'])
436+
def foo(request):
437+
return request.param
438+
"""
439+
)
440+
testdir.makepyfile(
441+
"""
442+
import pytest
443+
444+
@pytest.fixture(params=[1, 2])
445+
def foo(request):
446+
return request.param
447+
448+
class Test:
449+
450+
@pytest.fixture
451+
def foo(self, foo):
452+
return foo * 2
453+
454+
def test_spam(self, foo):
455+
assert foo in (2, 4)
456+
"""
457+
)
458+
result = testdir.runpytest()
459+
result.stdout.fnmatch_lines(["*2 passed*"])
460+
461+
def test_override_parametrized_fixture_with_new_parametrized_fixture(self, testdir):
462+
"""Overriding a parametrized fixture, while also parametrizing the new fixture and
463+
simultaneously requesting the overwritten fixture as parameter, yields the same value
464+
as ``request.param``.
465+
"""
466+
testdir.makeconftest(
467+
"""
468+
import pytest
469+
470+
@pytest.fixture(params=['ignored', 'ignored'])
471+
def foo(request):
472+
return request.param
473+
"""
474+
)
475+
testdir.makepyfile(
476+
"""
477+
import pytest
478+
479+
@pytest.fixture(params=[10, 20])
480+
def foo(foo, request):
481+
assert request.param == foo
482+
return foo * 2
483+
484+
def test_spam(foo):
485+
assert foo in (20, 40)
486+
"""
487+
)
488+
result = testdir.runpytest()
489+
result.stdout.fnmatch_lines(["*2 passed*"])
490+
399491
def test_autouse_fixture_plugin(self, testdir):
400492
# A fixture from a plugin has no baseid set, which screwed up
401493
# the autouse fixture handling.

0 commit comments

Comments
 (0)