0
0
FastAPIframework~10 mins

Overriding dependencies in tests 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 override a FastAPI dependency in a test.

FastAPI
app.dependency_overrides[[1]] = override_dependency
Drag options to blanks, or click blank then click option'
ADepends
Boverride_dependency
Capp
Dget_db
Attempts:
3 left
💡 Hint
Common Mistakes
Using the override function as the key instead of the original dependency.
Trying to override the app object itself.
2fill in blank
medium

Complete the test client setup to use the overridden dependencies.

FastAPI
client = TestClient([1])
Drag options to blanks, or click blank then click option'
Aapp
Boverride_dependency
Cget_db
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the override function instead of the app.
Passing the dependency function instead of the app.
3fill in blank
hard

Fix the error in the dependency override removal after the test.

FastAPI
app.dependency_overrides.[1](get_db)
Drag options to blanks, or click blank then click option'
Apop
Bclear
Cdelete
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove which is not a dictionary method.
Using delete which is not a method.
Using clear which removes all overrides.
4fill in blank
hard

Fill both blanks to define a dependency override function that yields a test database session.

FastAPI
def override_dependency():
    db = [1]()
    try:
        yield db
    finally:
        db.[2]()
Drag options to blanks, or click blank then click option'
ASessionLocal
Bclose
Ccommit
Drollback
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit or rollback instead of close.
Not calling the session factory to create a session.
5fill in blank
hard

Fill all three blanks to override a dependency, create a test client, and remove the override after the test.

FastAPI
app.dependency_overrides[[1]] = override_dependency
client = TestClient([2])
# run tests here
app.dependency_overrides.[3](get_db)
Drag options to blanks, or click blank then click option'
Aget_db
Bapp
Cpop
Doverride_dependency
Attempts:
3 left
💡 Hint
Common Mistakes
Using the override function as the key instead of the original dependency.
Passing the override function to TestClient instead of the app.
Using a wrong method like remove or delete instead of pop.