Skip to content

Commit 80b4f8b

Browse files
Fix a bug
1 parent bbf5949 commit 80b4f8b

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

src/_pytest/fixtures.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,11 +1414,10 @@ def getfixtureinfo(
14141414
tuple(self._getautousenames(node.nodeid)) + usefixtures + argnames
14151415
)
14161416

1417-
arg2fixturedefs: Dict[str, Sequence[FixtureDef[Any]]] = {}
1418-
names_closure = self.getfixtureclosure(
1417+
names_closure, arg2fixturedefs = self.getfixtureclosure(
14191418
node,
14201419
initialnames,
1421-
arg2fixturedefs,
1420+
None,
14221421
ignore_args=_get_direct_parametrize_args(node),
14231422
)
14241423
return FuncFixtureInfo(
@@ -1461,9 +1460,9 @@ def getfixtureclosure(
14611460
self,
14621461
parentnode: nodes.Node,
14631462
initialnames: Tuple[str, ...],
1464-
arg2fixturedefs: Dict[str, Sequence[FixtureDef[Any]]],
1463+
arg2fixturedefs: Union[Dict[str, Sequence[FixtureDef[Any]]], None],
14651464
ignore_args: Sequence[str] = (),
1466-
) -> List[str]:
1465+
) -> Tuple[List[str], Dict[str, Sequence[FixtureDef[Any]]]]:
14671466
# Collect the closure of all fixtures, starting with the given
14681467
# initialnames containing function arguments, `usefixture` markers
14691468
# and `autouse` fixtures as the initial set. As we have to visit all
@@ -1474,6 +1473,8 @@ def getfixtureclosure(
14741473

14751474
fixturenames_closure = initialnames
14761475

1476+
if arg2fixturedefs is None:
1477+
arg2fixturedefs = {}
14771478
lastlen = -1
14781479
parentid = parentnode.nodeid
14791480
while lastlen != len(fixturenames_closure):
@@ -1498,7 +1499,10 @@ def sort_by_scope(arg_name: str) -> Scope:
14981499
else:
14991500
return fixturedefs[-1]._scope
15001501

1501-
return sorted(fixturenames_closure, key=sort_by_scope, reverse=True)
1502+
return (
1503+
sorted(fixturenames_closure, key=sort_by_scope, reverse=True),
1504+
arg2fixturedefs,
1505+
)
15021506

15031507
def pytest_generate_tests(self, metafunc: "Metafunc") -> None:
15041508
"""Generate new tests based on parametrized fixtures used by the given metafunc"""

src/_pytest/python.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -381,21 +381,9 @@ class _EmptyClass: pass # noqa: E701
381381
# fmt: on
382382

383383

384-
def prune_dependency_tree_if_test_is_dynamically_parametrized(metafunc):
384+
def check_if_test_is_dynamically_parametrized(metafunc):
385385
if metafunc._calls:
386-
# Dynamic direct parametrization may have shadowed some fixtures
387-
# so make sure we update what the function really needs. Note that
388-
# we didn't need to do this if only indirect dynamic parametrization
389-
# had taken place, but anyway we did it as differentiating between direct
390-
# and indirect requires a dirty hack.
391-
definition = metafunc.definition
392-
fixture_closure = definition.parent.session._fixturemanager.getfixtureclosure(
393-
definition,
394-
definition._fixtureinfo.initialnames,
395-
definition._fixtureinfo.name2fixturedefs,
396-
ignore_args=_get_direct_parametrize_args(definition) + ["request"],
397-
)
398-
definition._fixtureinfo.names_closure[:] = fixture_closure
386+
setattr(metafunc, "has_dynamic_parametrize", True)
399387

400388

401389
class PyCollector(PyobjMixin, nodes.Collector):
@@ -502,7 +490,7 @@ def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
502490
module=module,
503491
_ispytest=True,
504492
)
505-
methods = [prune_dependency_tree_if_test_is_dynamically_parametrized]
493+
methods = [check_if_test_is_dynamically_parametrized]
506494
if hasattr(module, "pytest_generate_tests"):
507495
methods.append(module.pytest_generate_tests)
508496
if cls is not None and hasattr(cls, "pytest_generate_tests"):
@@ -516,6 +504,20 @@ def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
516504
yield Function.from_parent(self, name=name, fixtureinfo=fixtureinfo)
517505
else:
518506

507+
if hasattr(metafunc, "has_dynamic_parametrize"):
508+
# add_funcarg_pseudo_fixture_def may have shadowed some fixtures
509+
# due to dynamic direct parametrization so make sure we update
510+
# what the function really needs. Note that we didn't need to do this if
511+
# only indirect dynamic parametrization had taken place, but anyway we did
512+
# it as differentiating between direct and indirect requires a dirty hack.
513+
fixture_closure, _ = fm.getfixtureclosure(
514+
definition,
515+
fixtureinfo.initialnames,
516+
fixtureinfo.name2fixturedefs,
517+
ignore_args=_get_direct_parametrize_args(definition),
518+
)
519+
fixtureinfo.names_closure[:] = fixture_closure
520+
519521
for callspec in metafunc._calls:
520522
subname = f"{name}[{callspec.id}]"
521523
yield Function.from_parent(

testing/python/fixtures.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4537,7 +4537,9 @@ def test_fixt(custom):
45374537

45384538

45394539
@pytest.mark.xfail(
4540-
reason="arg2fixturedefs should get updated on dynamic parametrize. This gets solved by PR#11220"
4540+
reason="fixtureclosure should get updated before fixtures.py::pytest_generate_tests"
4541+
" and after modifying arg2fixturedefs when there's direct"
4542+
" dynamic parametrize. This gets solved by PR#11220"
45414543
)
45424544
def test_fixture_info_after_dynamic_parametrize(pytester: Pytester) -> None:
45434545
pytester.makeconftest(
@@ -4575,7 +4577,7 @@ def test(fixture2):
45754577
assert fixture2 in (4, 5)
45764578
"""
45774579
)
4578-
res = pytester.inline_run("-s")
4580+
res = pytester.inline_run()
45794581
res.assertoutcome(passed=2)
45804582

45814583

0 commit comments

Comments
 (0)