The fixture request object lets you get information about the current test and access other fixtures easily.
0
0
Fixture request object in PyTest
Introduction
When you want to use one fixture inside another fixture.
When you need details about the test function that is running.
When you want to dynamically get fixture names or parameters.
When you want to skip or fail a test from inside a fixture.
When you want to access test metadata like markers or config.
Syntax
PyTest
def fixture_name(request): # use request object here pass
The request object is passed as a parameter to a fixture function.
You can use request.getfixturevalue('fixture_name') to get another fixture.
Examples
This fixture prints the name of the test that uses it.
PyTest
import pytest @pytest.fixture def my_fixture(request): print(f"Running test: {request.node.name}") return 42
This fixture uses
request to get another fixture's value.PyTest
import pytest @pytest.fixture def dependent_fixture(request): other = request.getfixturevalue('my_fixture') return other + 1
Sample Program
This test uses the request object to get the base_value fixture inside add_five. It then checks the sum.
PyTest
import pytest @pytest.fixture def base_value(): return 10 @pytest.fixture def add_five(request): base = request.getfixturevalue('base_value') return base + 5 def test_sum(add_five): assert add_five == 15 print(f"add_five value is {add_five}")
OutputSuccess
Important Notes
The request object is only available inside fixtures, not in test functions directly.
Use request.node.name to get the current test function name.
Using request.getfixturevalue() helps avoid circular dependencies between fixtures.
Summary
The fixture request object gives info about the current test and fixtures.
It helps fixtures use other fixtures dynamically.
It is useful for accessing test names, markers, and config inside fixtures.