Skip to content

Commit 0b725e7

Browse files
Fix a bug and add a test
1 parent 80b4f8b commit 0b725e7

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/_pytest/fixtures.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,12 @@ def getfixtureclosure(
14861486
fixturedefs = self.getfixturedefs(argname, parentid)
14871487
if fixturedefs:
14881488
arg2fixturedefs[argname] = fixturedefs
1489-
if argname in arg2fixturedefs:
1489+
else:
1490+
fixturedefs = arg2fixturedefs[argname]
1491+
if fixturedefs and not (
1492+
fixturedefs[-1].func.__name__ == "get_direct_param_fixture_func"
1493+
and fixturedefs[-1].baseid == ""
1494+
):
14901495
fixturenames_closure = deduplicate_names(
14911496
fixturenames_closure + arg2fixturedefs[argname][-1].argnames
14921497
)

testing/python/fixtures.py

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

46214621

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

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)