0
0
FastAPIframework~20 mins

Fixture organization in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does fixture scope affect FastAPI test client reuse?

Consider a FastAPI test setup where a client fixture is defined with different scopes. What happens when the fixture scope is set to function versus module?

FastAPI
import pytest
from fastapi.testclient import TestClient
from myapp import app

@pytest.fixture(scope="function")
def client():
    return TestClient(app)

# In tests, client is used to make requests
AWith scope='module', a new TestClient instance is created for every test invocation, causing slower tests.
BWith scope='function', a new TestClient instance is created for each test function, isolating state between tests.
CWith scope='function', the same TestClient instance is reused across all test functions in the module.
DWith scope='module', a new TestClient instance is created for each test function, increasing test isolation.
Attempts:
2 left
💡 Hint

Think about how pytest fixture scopes control the lifetime of the fixture object.

state_output
intermediate
2:00remaining
What is the output when a fixture modifies shared state incorrectly?

Given this fixture and test code, what will be the value of counter['count'] after running both tests?

FastAPI
import pytest

counter = {'count': 0}

@pytest.fixture(scope='module')
def shared_counter():
    counter['count'] += 1
    return counter

def test_one(shared_counter):
    assert shared_counter['count'] == 1

def test_two(shared_counter):
    assert shared_counter['count'] == 1
Acounter['count'] will be 1 after both tests run because the fixture runs once per module and increments once.
Bcounter['count'] will be 0 because the fixture does not modify the counter correctly.
Ccounter['count'] will be 2 after both tests run because the fixture runs once per module.
DTests will fail because the fixture is not returning a fresh dictionary each time.
Attempts:
2 left
💡 Hint

Remember that scope='module' means the fixture runs once per module, so the increment happens once.

📝 Syntax
advanced
2:00remaining
Which fixture definition causes a syntax error in FastAPI tests?

Identify the fixture definition that will cause a syntax error when used in FastAPI tests with pytest.

A
@pytest.fixture(scope='module')
def client():
    return TestClient(app)
B
)ppa(tneilCtseT nruter    
:)(tneilc fed
)'noitcnuf'=epocs(erutxif.tsetyp@
C
@pytest.fixture(scope='function')
def client():
    return TestClient(app)
D
@pytest.fixture
def client():
    return TestClient(app)
Attempts:
2 left
💡 Hint

Check the function definition syntax carefully.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI fixture cause tests to share unwanted state?

Examine the fixture below. Why do tests using this fixture share state unexpectedly?

FastAPI
import pytest

class Resource:
    def __init__(self):
        self.value = 0

resource = Resource()

@pytest.fixture(scope='module')
def shared_resource():
    return resource

def test_a(shared_resource):
    shared_resource.value += 1
    assert shared_resource.value == 1

def test_b(shared_resource):
    assert shared_resource.value == 0
AThe fixture returns a global object, so changes persist across tests sharing the fixture.
BThe fixture scope is 'module', so a new Resource instance is created for each test.
CThe Resource class is immutable, so state cannot be changed between tests.
DThe fixture is missing a yield statement, causing state sharing.
Attempts:
2 left
💡 Hint

Think about how returning a global object affects test isolation.

🧠 Conceptual
expert
3:00remaining
What is the best way to organize database fixtures in FastAPI tests for isolation and performance?

In FastAPI testing, you want to organize your database fixtures to ensure each test runs with a clean state but also keep tests fast. Which approach best balances isolation and performance?

AUse a <code>function</code> scoped fixture that reuses the same database connection without cleanup.
BUse a <code>function</code> scoped fixture that creates a fresh test database and applies migrations before each test.
CUse a <code>session</code> scoped fixture that creates a test database once per test session and does not reset state between tests.
DUse a <code>module</code> scoped fixture that creates a test database once per module and rolls back transactions after each test.
Attempts:
2 left
💡 Hint

Consider how transaction rollbacks can speed up tests while keeping data isolated.