Bird
0
0

You want to test a Flask app that uses a database. Which sequence correctly sets up and cleans the test database for each test?

hard📝 state output Q15 of 15
Flask - Testing Flask Applications
You want to test a Flask app that uses a database. Which sequence correctly sets up and cleans the test database for each test?
ACall <code>db.create_all()</code> in setUp, run test, then <code>db.drop_all()</code> in tearDown
BCall <code>db.drop_all()</code> in setUp, run test, then <code>db.create_all()</code> in tearDown
CCall <code>db.session.commit()</code> in setUp, then <code>db.session.remove()</code> in tearDown
DCall <code>db.create_all()</code> and <code>db.drop_all()</code> both in setUp
Step-by-Step Solution
Solution:
  1. Step 1: Understand test setup and cleanup

    Creating tables before each test ensures a fresh database state.
  2. Step 2: Understand test teardown

    Dropping tables after each test cleans up to avoid data carryover.
  3. Step 3: Match correct sequence

    Calling create_all() in setUp and drop_all() in tearDown is the correct pattern.
  4. Final Answer:

    Call db.create_all() in setUp, run test, then db.drop_all() in tearDown -> Option A
  5. Quick Check:

    Create tables before, drop after test = A [OK]
Quick Trick: Create tables before test, drop after test [OK]
Common Mistakes:
MISTAKES
  • Dropping tables before test instead of after
  • Calling create_all and drop_all both in setUp
  • Confusing commit with setup/teardown

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes