test_getfixturevalue_dynamic.py
Overview
This file contains a minimal set of `pytest` fixtures and a test function designed to demonstrate and verify `pytest`'s dynamic fixture resolution using [request.getfixturevalue()](/projects/286/67370). It specifically tests how fixtures can be retrieved and composed dynamically at runtime, and it checks the order and presence of fixture names in the test context.
The file is primarily intended for exploring `pytest` internals related to fixture dependency resolution, especially when fixtures call [request.getfixturevalue()](/projects/286/67370) to access other fixtures at runtime instead of declaring them in their signature.
Detailed Explanation
Imports
pytest: The testing framework used to define fixtures and test functions.from __future__ import annotations: Enables postponed evaluation of annotations (not critical here but included).
Fixtures
dynamic
@pytest.fixture
def dynamic():
pass
Purpose: A simple fixture with no implementation (empty body).
Parameters: None.
Returns: None.
Usage: Serves as a leaf fixture to be dynamically retrieved by other fixtures.
Notes: Although it does nothing, its presence is important for demonstrating dynamic fixture retrieval.
a
@pytest.fixture
def a(request):
request.getfixturevalue("dynamic")
Purpose: A fixture that dynamically retrieves the
dynamicfixture at runtime.Parameters:
request: A built-inpytestfixture that provides information about the executing test function and access to other fixtures.
Returns: None.
Usage: Instead of declaring
dynamicas a parameter, it usesrequest.getfixturevalue("dynamic")to access it.Implementation detail: This pattern allows conditional or programmatic fixture retrieval rather than static dependency declaration.
b
@pytest.fixture
def b(a):
pass
Purpose: A fixture that depends on fixture
a.Parameters:
a: Fixtureais injected automatically.
Returns: None.
Usage: Demonstrates chaining dependencies, where
bdepends ona, andadynamically depends ondynamic.
Test Function
test
def test(b, request):
assert request.fixturenames == ["b", "request", "a", "dynamic"]
Purpose: Tests the order and presence of fixture names available in the test context.
Parameters:
b: Fixturebinjected (which depends ona, which usesdynamicdynamically).request: Pytest fixture providing test context.
Returns: None.
Behavior:
Asserts that the
request.fixturenameslist contains the fixtures in the order:["b", "request", "a", "dynamic"].This shows that even though
dynamicwas not declared as a parameter in a fixture signature directly, it is recognized in the fixture list due to dynamic access.
Usage Example: This function can be run by pytest to validate fixture resolution.
Important Implementation Details and Algorithms
Dynamic Fixture Retrieval: The key feature demonstrated is the use of
request.getfixturevalue("dynamic")inside fixturea. This bypasses static fixture dependency declaration and allows fixtures to request other fixtures dynamically during setup.Fixture Dependency Chain: Fixture
bdepends ona, andadepends ondynamicdynamically. This chain ensures that whenbis requested, all underlying fixtures are resolved properly.Fixture Name Collection: The test verifies that
pytestcollects all fixture names involved in the test invocation, including those accessed dynamically.
Interaction with Other System Parts
This file is a self-contained pytest test module.
It interacts with
pytest's fixture management system and test runner.It may be part of a larger test suite to verify or demonstrate
pytestinternals or to ensure that dynamic fixture retrieval behaves as expected.No external modules or system components are used beyond pytest itself.
Visual Diagram
classDiagram
class Fixture_dynamic {
+dynamic()
}
class Fixture_a {
+a(request)
- calls getfixturevalue("dynamic")
}
class Fixture_b {
+b(a)
}
class TestFunction_test {
+test(b, request)
- asserts fixture name order
}
Fixture_b --> Fixture_a : depends on
Fixture_a ..> Fixture_dynamic : dynamically uses
TestFunction_test --> Fixture_b : uses
TestFunction_test --> pytest.Request : uses
Summary
Entity | Description | Parameters | Returns | Notes |
|---|---|---|---|---|
`dynamic` | Empty fixture; leaf node in fixture graph. | None | None | Dynamically accessed by `a`. |
`a` | Fixture dynamically calls `dynamic` fixture. | `request`: pytest Request | None | Uses `request.getfixturevalue("dynamic")`. |
`b` | Fixture depending on `a`. | `a`: fixture | None | Demonstrates dependency chaining. |
`test` | Test function asserting fixture name order. | `b`, `request` | None | Verifies that dynamic fixtures appear in list. |
Usage Notes
Running pytest test_getfixturevalue_dynamic.py will execute the test verifying fixture resolution.
This pattern is useful when fixture dependencies are not known upfront or need conditional logic.
The test confirms that
pytestcorrectly tracks dynamically retrieved fixtures in the fixture list.