0
0
PyTesttesting~20 mins

@pytest.fixture decorator - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pytest Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a test using a simple fixture
What is the output when running this pytest test with the given fixture?
PyTest
import pytest

@pytest.fixture
def sample_data():
    return [1, 2, 3]

def test_sum(sample_data):
    assert sum(sample_data) == 6
    print("Test passed")
ATest passed\nFAILED
BSyntaxError
CTest passed\nPASSED
DFAILED
Attempts:
2 left
💡 Hint
Look at what the fixture returns and what the test asserts.
assertion
intermediate
1:30remaining
Correct assertion with fixture data
Given this fixture, which assertion correctly verifies the fixture data?
PyTest
import pytest

@pytest.fixture
def user_info():
    return {"name": "Alice", "age": 30}
Aassert user_info["age"] == "30"
Bassert user_info.name == "Alice"
Cassert user_info.get("age") == 25
Dassert user_info["name"] == "Alice"
Attempts:
2 left
💡 Hint
Remember how to access dictionary values in Python.
🔧 Debug
advanced
2:00remaining
Identify the error in fixture usage
What error occurs when running this test code?
PyTest
import pytest

@pytest.fixture
def data():
    return [1, 2, 3]

def test_data():
    assert sum(data) == 6
AAssertionError
BTypeError: 'function' object is not iterable
CNameError: name 'data' is not defined
DNo error, test passes
Attempts:
2 left
💡 Hint
Check how the fixture is used inside the test function.
🧠 Conceptual
advanced
1:00remaining
Scope of pytest fixtures
Which fixture scope causes the fixture to be created once per test session?
Asession
Bclass
Cfunction
Dmodule
Attempts:
2 left
💡 Hint
Think about how long the fixture lives during test runs.
framework
expert
1:30remaining
Using autouse fixtures in pytest
What is the effect of setting autouse=True in a pytest fixture?
AThe fixture runs automatically for all tests without needing to be requested
BThe fixture runs only once per test class
CThe fixture is skipped during test runs
DThe fixture runs only when explicitly requested by test functions
Attempts:
2 left
💡 Hint
Consider how autouse changes fixture invocation.