Test Overview
This test uses the pytest fixture request object to access the name of the test function dynamically. It verifies that the fixture correctly provides the test function's name during execution.
This test uses the pytest fixture request object to access the name of the test function dynamically. It verifies that the fixture correctly provides the test function's name during execution.
import pytest @pytest.fixture def fixture_with_request(request): # Access the name of the test function using request return request.function.__name__ def test_example(fixture_with_request): # Assert that the fixture returns the current test function name assert fixture_with_request == "test_example"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initializes | — | PASS |
| 2 | pytest collects test_example function | Test function test_example is ready to run | — | PASS |
| 3 | pytest calls fixture_with_request fixture with request object | Fixture receives request object containing test context | request.function.__name__ is 'test_example' | PASS |
| 4 | Fixture returns the test function name 'test_example' | Fixture output is 'test_example' | — | PASS |
| 5 | test_example function runs with fixture_with_request value | test_example receives 'test_example' from fixture | assert fixture_with_request == 'test_example' | PASS |
| 6 | Test completes successfully | Test passed with no errors | — | PASS |
request object in pytest fixtures?request object in pytest is passed to fixtures to give them context about the current test being run.request object?request object has a node attribute representing the test item.request.node.name.test_example runs?
import pytest
@pytest.fixture
def show_test_name(request):
print(f"Running test: {request.node.name}")
def test_example(show_test_name):
assert True
show_test_name prints the current test function name using request.node.name.test_example, so request.node.name will be "test_example".request object:
@pytest.fixture
def check_marker(request):
marker = request.marker.get('slow')
if marker:
print("Slow test")
request.node.get_closest_marker('name'), not request.marker.request.node.get_closest_marker('slow') to get the marker.request object inside a fixture to dynamically use another fixture by name?request object.request.getfixturevalue('fixture_name').Pass the test and this pattern moves unknown