Skip to content

Commit

Permalink
Fix a bug and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
sadra-barikbin committed Jul 31, 2023
1 parent c61cc9a commit b92c1a5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def add_funcarg_pseudo_fixture_def(
# if they use direct funcargs (i.e. direct parametrization)
# because we want later test execution to be able to rely on
# an existing FixtureDef structure for all arguments.
# XXX we can probably avoid this algorithm if we modify CallSpec2
# XXX we can probably avoid this algorithm if we modify CallSpec2
# to directly care for creating the fixturedefs within its methods.
if not metafunc._calls[0].funcargs:
# This function call does not have direct parametrization.
Expand Down Expand Up @@ -1564,7 +1564,12 @@ def getfixtureclosure(
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
)
Expand Down
26 changes: 26 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4621,6 +4621,32 @@ def test_2(fixture1):
)


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(
"""
Expand Down
37 changes: 37 additions & 0 deletions testing/python/show_fixtures_per_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from _pytest.pytester import Pytester


Expand Down Expand Up @@ -252,3 +253,39 @@ def test_arg1(arg1):
" 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,
)

0 comments on commit b92c1a5

Please sign in to comment.