Skip to content

Commit c8a4618

Browse files
authored
Merge pull request #13798 from bluetech/fixtures-prune-test
testing: add a test demonstrating why `prune_dependency_tree` is needed
2 parents 1bed1d7 + 18c0f75 commit c8a4618

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

testing/python/fixtures.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5071,6 +5071,42 @@ def test_method(self, /, fix):
50715071
result.assert_outcomes(passed=1)
50725072

50735073

5074+
def test_parametrization_dependency_pruning(pytester: Pytester) -> None:
5075+
"""Test that when a fixture is dynamically shadowed by parameterization, it
5076+
is properly pruned and not executed."""
5077+
pytester.makepyfile(
5078+
"""
5079+
import pytest
5080+
5081+
5082+
# This fixture should never run because shadowed_fixture is parametrized.
5083+
@pytest.fixture
5084+
def boom():
5085+
raise RuntimeError("BOOM!")
5086+
5087+
5088+
# This fixture is shadowed by metafunc.parametrize in pytest_generate_tests.
5089+
@pytest.fixture
5090+
def shadowed_fixture(boom):
5091+
return "fixture_value"
5092+
5093+
5094+
# Dynamically parametrize shadowed_fixture, replacing the fixture with direct values.
5095+
def pytest_generate_tests(metafunc):
5096+
if "shadowed_fixture" in metafunc.fixturenames:
5097+
metafunc.parametrize("shadowed_fixture", ["param1", "param2"])
5098+
5099+
5100+
# This test should receive shadowed_fixture as a parametrized value, and
5101+
# boom should not explode.
5102+
def test_shadowed(shadowed_fixture):
5103+
assert shadowed_fixture in ["param1", "param2"]
5104+
"""
5105+
)
5106+
result = pytester.runpytest()
5107+
result.assert_outcomes(passed=2)
5108+
5109+
50745110
def test_fixture_closure_with_overrides(pytester: Pytester) -> None:
50755111
"""Test that an item's static fixture closure properly includes transitive
50765112
dependencies through overridden fixtures (#13773)."""

0 commit comments

Comments
 (0)