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 |