0
0
FastAPIframework~10 mins

Fixture organization in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple FastAPI fixture that provides a test client.

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

@pytest.fixture
 def client():
    return [1](app)
Drag options to blanks, or click blank then click option'
ATestClient
BFastAPI
CClientSession
DHttpClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using FastAPI class instead of TestClient
Using unrelated HTTP client classes
2fill in blank
medium

Complete the fixture to yield a database session for tests.

FastAPI
import pytest
from myapp.database import SessionLocal

@pytest.fixture
 def db_session():
    db = [1]()
    try:
        yield db
    finally:
        db.close()
Drag options to blanks, or click blank then click option'
ASessionLocal
Bcreate_session
CDatabaseSession
Dget_db
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not create a session
Forgetting to close the session
3fill in blank
hard

Fix the error in this fixture that overrides the dependency for the database session.

FastAPI
from fastapi.testclient import TestClient
from myapp.main import app
from myapp.database import SessionLocal
import pytest
from myapp.database import get_db

@pytest.fixture
 def client_override_db():
    def override_get_db():
        db = SessionLocal()
        try:
            yield db
        finally:
            db.close()

    app.dependency_overrides[[1]] = override_get_db
    with TestClient(app) as c:
        yield c
    app.dependency_overrides.clear()
Drag options to blanks, or click blank then click option'
Aoverride_get_db
BSessionLocal
Cget_db
Ddb_session
Attempts:
3 left
💡 Hint
Common Mistakes
Using the session class as the key
Using the override function as the key
4fill in blank
hard

Fill both blanks to create a fixture that resets the database before each test.

FastAPI
import pytest
from myapp.database import Base, engine

@pytest.fixture(autouse=True)
 def reset_db():
    Base.metadata.[1](bind=engine)
    Base.metadata.[2](bind=engine)
Drag options to blanks, or click blank then click option'
Adrop_all
Bcreate_all
Cinitialize
Dreset
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order of drop_all and create_all
Using non-existent methods like initialize or reset
5fill in blank
hard

Fill all three blanks to define a fixture that provides an authenticated test client.

FastAPI
import pytest
from fastapi.testclient import TestClient
from myapp.main import app
from myapp.auth import create_access_token

@pytest.fixture
 def auth_client():
    token = create_access_token(data={"sub": [1])
    headers = {"Authorization": f"Bearer [2]"}
    client = TestClient(app)
    client.headers.update([3])
    return client
Drag options to blanks, or click blank then click option'
A"testuser"
Btoken
Cheaders
D"admin"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong username string
Not updating client headers correctly