Test Overview
This test demonstrates how to select a fixture dynamically in pytest based on a test parameter. It verifies that the correct fixture is used and returns the expected value.
This test demonstrates how to select a fixture dynamically in pytest based on a test parameter. It verifies that the correct fixture is used and returns the expected value.
import pytest @pytest.fixture def fixture_a(): return "Data from fixture A" @pytest.fixture def fixture_b(): return "Data from fixture B" @pytest.fixture def dynamic_fixture(request): if request.param == "a": return request.getfixturevalue("fixture_a") elif request.param == "b": return request.getfixturevalue("fixture_b") else: pytest.skip("Unsupported fixture parameter") @pytest.mark.parametrize("dynamic_fixture", ["a", "b"], indirect=True) def test_dynamic_fixture(dynamic_fixture): assert dynamic_fixture in ["Data from fixture A", "Data from fixture B"]
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | pytest collects test_dynamic_fixture with parameter 'a' | Test function ready to run with dynamic_fixture param 'a' | — | PASS |
| 3 | pytest calls dynamic_fixture fixture with param 'a' | Inside dynamic_fixture, param is 'a' | — | PASS |
| 4 | dynamic_fixture calls fixture_a using getfixturevalue | fixture_a returns 'Data from fixture A' | — | PASS |
| 5 | test_dynamic_fixture runs with dynamic_fixture='Data from fixture A' | Test function executing | Assert dynamic_fixture is in ['Data from fixture A', 'Data from fixture B'] | PASS |
| 6 | pytest collects test_dynamic_fixture with parameter 'b' | Test function ready to run with dynamic_fixture param 'b' | — | PASS |
| 7 | pytest calls dynamic_fixture fixture with param 'b' | Inside dynamic_fixture, param is 'b' | — | PASS |
| 8 | dynamic_fixture calls fixture_b using getfixturevalue | fixture_b returns 'Data from fixture B' | — | PASS |
| 9 | test_dynamic_fixture runs with dynamic_fixture='Data from fixture B' | Test function executing | Assert dynamic_fixture is in ['Data from fixture A', 'Data from fixture B'] | PASS |
pytest?request.getfixturevalue()?getfixturevalue, not getfixture.test_dynamic_fixture?
import pytest
@pytest.fixture
def fixture_a():
return 'A'
@pytest.fixture
def fixture_b():
return 'B'
@pytest.mark.parametrize('use_a', [True, False])
def test_dynamic_fixture(request, use_a):
fixture_name = 'fixture_a' if use_a else 'fixture_b'
value = request.getfixturevalue(fixture_name)
print(value)
assert value in ['A', 'B']
import pytest
@pytest.fixture
def fix1():
return 10
@pytest.fixture
def fix2():
return 20
@pytest.mark.parametrize('name', ['fix1', 'fix2'])
def test_select(request, name):
value = request.getfixture(name)
assert value in [10, 20]
getfixturevalue, not getfixture.DB_TYPE. Which approach correctly implements dynamic fixture selection in pytest?
A) Use request.getfixturevalue() inside the test to select the fixture by name.
B) Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones.
C) Use pytest_generate_tests() hook to parametrize fixtures.
D) Use yield in fixtures to switch dynamically.request.getfixturevalue() to pick the fixture by name at runtime, which fits the need.Pass the test and this pattern moves unknown