Challenge - 5 Problems
Fixture Request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing fixture name via request object
What is the output of this pytest fixture and test code?
PyTest
import pytest @pytest.fixture def sample_fixture(request): return f"Fixture name is: {request.fixturename}" def test_sample(sample_fixture): print(sample_fixture)
Attempts:
2 left
💡 Hint
The request object has an attribute that holds the current fixture's name.
✗ Incorrect
The 'request' fixture does not have an attribute named 'fixturename'. The correct attribute is 'fixture_name'. Accessing 'fixturename' raises an AttributeError.
❓ assertion
intermediate1:30remaining
Correct assertion using request.node.name in a test
Which assertion correctly verifies the current test function's name using the request fixture?
PyTest
import pytest def test_example(request): test_name = request.node.name # Assertion here
Attempts:
2 left
💡 Hint
request.node.name holds the current test function's name.
✗ Incorrect
The 'request.node.name' attribute contains the name of the currently running test function, which is 'test_example'.
🔧 Debug
advanced2:00remaining
Identify the error when accessing request.param in a fixture
What error will this pytest code raise when running the test?
PyTest
import pytest @pytest.fixture def my_fixture(request): return request.param def test_func(my_fixture): assert my_fixture == 10
Attempts:
2 left
💡 Hint
request.param is only available for parameterized fixtures.
✗ Incorrect
The fixture uses request.param but is not parameterized with @pytest.mark.parametrize, so pytest raises a usage error indicating missing parameterization.
🧠 Conceptual
advanced1:30remaining
Purpose of the pytest request fixture
Which statement best describes the purpose of the pytest 'request' fixture?
Attempts:
2 left
💡 Hint
Think about what information the 'request' fixture gives inside a test or fixture.
✗ Incorrect
The 'request' fixture gives access to the current test context, including test name, fixture names, and allows dynamic fixture requests.
❓ framework
expert2:30remaining
Using request.getfixturevalue to access another fixture dynamically
Given these fixtures, what will be the output of test_dynamic_fixture?
PyTest
import pytest @pytest.fixture def data_fixture(): return 42 @pytest.fixture def dynamic_fixture(request): value = request.getfixturevalue('data_fixture') return value + 8 def test_dynamic_fixture(dynamic_fixture): print(dynamic_fixture)
Attempts:
2 left
💡 Hint
request.getfixturevalue allows accessing other fixtures by name inside a fixture.
✗ Incorrect
The 'dynamic_fixture' calls request.getfixturevalue('data_fixture') which returns 42, then adds 8, so the output is 50.