Skip to content

Commit b3cd38e

Browse files
authored
Merge pull request #13966 from bluetech/fixtures-rm-special-case
fixtures: a few small cleanups + a TODO
2 parents 9652bc7 + 088718a commit b3cd38e

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

src/_pytest/fixtures.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
from _pytest.deprecated import MARKED_FIXTURE
5757
from _pytest.deprecated import YIELD_FIXTURE
5858
from _pytest.main import Session
59-
from _pytest.mark import Mark
6059
from _pytest.mark import ParameterSet
6160
from _pytest.mark.structures import MarkDecorator
6261
from _pytest.outcomes import fail
@@ -346,11 +345,6 @@ def prune_dependency_tree(self) -> None:
346345
working_set = set(self.initialnames)
347346
while working_set:
348347
argname = working_set.pop()
349-
# Argname may be something not included in the original names_closure,
350-
# in which case we ignore it. This currently happens with pseudo
351-
# FixtureDefs which wrap 'get_direct_param_fixture_func(request)'.
352-
# So they introduce the new dependency 'request' which might have
353-
# been missing in the original tree (closure).
354348
if argname not in closure and argname in self.names_closure:
355349
closure.add(argname)
356350
if argname in self.name2fixturedefs:
@@ -1695,26 +1689,9 @@ def sort_by_scope(arg_name: str) -> Scope:
16951689

16961690
def pytest_generate_tests(self, metafunc: Metafunc) -> None:
16971691
"""Generate new tests based on parametrized fixtures used by the given metafunc"""
1698-
1699-
def get_parametrize_mark_argnames(mark: Mark) -> Sequence[str]:
1700-
args, _ = ParameterSet._parse_parametrize_args(*mark.args, **mark.kwargs)
1701-
return args
1702-
17031692
for argname in metafunc.fixturenames:
17041693
# Get the FixtureDefs for the argname.
1705-
fixture_defs = metafunc._arg2fixturedefs.get(argname)
1706-
if not fixture_defs:
1707-
# Will raise FixtureLookupError at setup time if not parametrized somewhere
1708-
# else (e.g @pytest.mark.parametrize)
1709-
continue
1710-
1711-
# If the test itself parametrizes using this argname, give it
1712-
# precedence.
1713-
if any(
1714-
argname in get_parametrize_mark_argnames(mark)
1715-
for mark in metafunc.definition.iter_markers("parametrize")
1716-
):
1717-
continue
1694+
fixture_defs = metafunc._arg2fixturedefs.get(argname, ())
17181695

17191696
# In the common case we only look at the fixture def with the
17201697
# closest scope (last in the list). But if the fixture overrides
@@ -1733,6 +1710,10 @@ def get_parametrize_mark_argnames(mark: Mark) -> Sequence[str]:
17331710
break
17341711

17351712
# Not requesting the overridden super fixture, stop.
1713+
#
1714+
# TODO: Handle the case where the super-fixture is transitively
1715+
# requested (see #7737 and the xfail'd test
1716+
# test_override_parametrized_fixture_via_transitive_fixture).
17361717
if argname not in fixturedef.argnames:
17371718
break
17381719

testing/python/fixtures.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,40 @@ def test_spam(foo):
590590
result = pytester.runpytest()
591591
result.stdout.fnmatch_lines(["*2 passed*"])
592592

593+
@pytest.mark.xfail(reason="not handled currently")
594+
def test_override_parametrized_fixture_via_transitive_fixture(
595+
self, pytester: Pytester
596+
) -> None:
597+
"""Test that overriding a parametrized fixture works even the super
598+
fixture is requested only transitively.
599+
600+
Regression test for #7737.
601+
"""
602+
pytester.makepyfile(
603+
"""
604+
import pytest
605+
606+
@pytest.fixture(params=[1, 2])
607+
def foo(request):
608+
return request.param
609+
610+
@pytest.fixture
611+
def bar(foo):
612+
return foo
613+
614+
class TestIt:
615+
@pytest.fixture
616+
def foo(self, bar):
617+
return bar * 2
618+
619+
def test_it(self, foo):
620+
pass
621+
"""
622+
)
623+
result = pytester.runpytest()
624+
assert result.ret == ExitCode.OK
625+
result.assert_outcomes(passed=2)
626+
593627
def test_autouse_fixture_plugin(self, pytester: Pytester) -> None:
594628
# A fixture from a plugin has no baseid set, which screwed up
595629
# the autouse fixture handling.

0 commit comments

Comments
 (0)