Skip to content

Commit b92c1a5

Browse files
Fix a bug and add a test
1 parent c61cc9a commit b92c1a5

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/_pytest/fixtures.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def add_funcarg_pseudo_fixture_def(
160160
# if they use direct funcargs (i.e. direct parametrization)
161161
# because we want later test execution to be able to rely on
162162
# an existing FixtureDef structure for all arguments.
163-
# XXX we can probably avoid this algorithm if we modify CallSpec2
163+
# XXX we can probably avoid this algorithm if we modify CallSpec2
164164
# to directly care for creating the fixturedefs within its methods.
165165
if not metafunc._calls[0].funcargs:
166166
# This function call does not have direct parametrization.
@@ -1564,7 +1564,12 @@ def getfixtureclosure(
15641564
fixturedefs = self.getfixturedefs(argname, parentid)
15651565
if fixturedefs:
15661566
arg2fixturedefs[argname] = fixturedefs
1567-
if argname in arg2fixturedefs:
1567+
else:
1568+
fixturedefs = arg2fixturedefs[argname]
1569+
if fixturedefs and not (
1570+
fixturedefs[-1].func.__name__ == "get_direct_param_fixture_func"
1571+
and fixturedefs[-1].baseid == ""
1572+
):
15681573
fixturenames_closure = deduplicate_names(
15691574
fixturenames_closure + arg2fixturedefs[argname][-1].argnames
15701575
)

testing/python/fixtures.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4621,6 +4621,32 @@ def test_2(fixture1):
46214621
)
46224622

46234623

4624+
def test_request_shouldnt_be_in_closure_after_pruning_dep_tree_when_its_not_in_initial_closure(
4625+
pytester: Pytester,
4626+
):
4627+
pytester.makepyfile(
4628+
"""
4629+
import pytest
4630+
4631+
def pytest_generate_tests(metafunc):
4632+
metafunc.parametrize("arg", [0])
4633+
4634+
@pytest.fixture()
4635+
def fixture():
4636+
pass
4637+
4638+
def test(fixture, arg):
4639+
pass
4640+
"""
4641+
)
4642+
result = pytester.runpytest("--setup-show")
4643+
result.stdout.re_match_lines(
4644+
[
4645+
r".+test\[0\] \(fixtures used: arg, fixture\)\.",
4646+
],
4647+
)
4648+
4649+
46244650
def test_dont_recompute_dependency_tree_if_no_dynamic_parametrize(pytester: Pytester):
46254651
pytester.makeconftest(
46264652
"""

testing/python/show_fixtures_per_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from _pytest.pytester import Pytester
23

34

@@ -252,3 +253,39 @@ def test_arg1(arg1):
252253
" Docstring content that extends into a third paragraph.",
253254
]
254255
)
256+
257+
258+
@pytest.mark.xfail(
259+
reason="python.py::show_fixtures_per_test uses arg2fixturedefs instead of fixtureclosure"
260+
)
261+
def test_should_not_show_fixtures_pruned_after_dynamic_parametrization(
262+
pytester: Pytester,
263+
) -> None:
264+
pytester.makepyfile(
265+
"""
266+
import pytest
267+
268+
@pytest.fixture
269+
def fixture1():
270+
pass
271+
272+
@pytest.fixture
273+
def fixture2(fixture1):
274+
pass
275+
276+
@pytest.fixture
277+
def fixture3(fixture2):
278+
pass
279+
280+
def pytest_generate_tests(metafunc):
281+
metafunc.parametrize("fixture3", [0])
282+
283+
def test(fixture3):
284+
pass
285+
"""
286+
)
287+
result = pytester.runpytest("--fixtures-per-test")
288+
result.stdout.re_match_lines(
289+
[r"-+ fixtures used by test\[0\] -+", r"-+ \(.+\) -+", r"fixture3 -- .+"],
290+
consecutive=True,
291+
)

0 commit comments

Comments
 (0)