From 0b725e70ab135b47126992980cfe7a781e9847db Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Mon, 31 Jul 2023 04:24:52 +0330 Subject: [PATCH] Fix a bug and add a test --- src/_pytest/fixtures.py | 7 ++++- testing/python/fixtures.py | 26 +++++++++++++++++ testing/python/show_fixtures_per_test.py | 37 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index c9240a474..d29fe21a4 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1486,7 +1486,12 @@ class FixtureManager: fixturedefs = self.getfixturedefs(argname, parentid) if fixturedefs: arg2fixturedefs[argname] = fixturedefs - if argname in arg2fixturedefs: + else: + fixturedefs = arg2fixturedefs[argname] + if fixturedefs and not ( + fixturedefs[-1].func.__name__ == "get_direct_param_fixture_func" + and fixturedefs[-1].baseid == "" + ): fixturenames_closure = deduplicate_names( fixturenames_closure + arg2fixturedefs[argname][-1].argnames ) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index ec0d44284..ca0183528 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -4619,6 +4619,32 @@ def test_reordering_after_dynamic_parametrize(pytester: Pytester): ) +def test_request_shouldnt_be_in_closure_after_pruning_dep_tree_when_its_not_in_initial_closure( + pytester: Pytester, +): + pytester.makepyfile( + """ + import pytest + + def pytest_generate_tests(metafunc): + metafunc.parametrize("arg", [0]) + + @pytest.fixture() + def fixture(): + pass + + def test(fixture, arg): + pass + """ + ) + result = pytester.runpytest("--setup-show") + result.stdout.re_match_lines( + [ + r".+test\[0\] \(fixtures used: arg, fixture\)\.", + ], + ) + + def test_dont_recompute_dependency_tree_if_no_dynamic_parametrize(pytester: Pytester): pytester.makeconftest( """ diff --git a/testing/python/show_fixtures_per_test.py b/testing/python/show_fixtures_per_test.py index f756dca41..c7b6ad701 100644 --- a/testing/python/show_fixtures_per_test.py +++ b/testing/python/show_fixtures_per_test.py @@ -1,3 +1,4 @@ +import pytest from _pytest.pytester import Pytester @@ -252,3 +253,39 @@ def test_verbose_include_multiline_docstring(pytester: Pytester) -> None: " Docstring content that extends into a third paragraph.", ] ) + + +@pytest.mark.xfail( + reason="python.py::show_fixtures_per_test uses arg2fixturedefs instead of fixtureclosure" +) +def test_should_not_show_fixtures_pruned_after_dynamic_parametrization( + pytester: Pytester, +) -> None: + pytester.makepyfile( + """ + import pytest + + @pytest.fixture + def fixture1(): + pass + + @pytest.fixture + def fixture2(fixture1): + pass + + @pytest.fixture + def fixture3(fixture2): + pass + + def pytest_generate_tests(metafunc): + metafunc.parametrize("fixture3", [0]) + + def test(fixture3): + pass + """ + ) + result = pytester.runpytest("--fixtures-per-test") + result.stdout.re_match_lines( + [r"-+ fixtures used by test\[0\] -+", r"-+ \(.+\) -+", r"fixture3 -- .+"], + consecutive=True, + )