0
0
Flaskframework~10 mins

Testing with database in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Testing with database
Setup Test Database
Create Test Client
Run Test Function
Insert Test Data
Perform Test Actions
Check Results
Teardown Test Database
END
This flow shows how Flask tests use a separate test database, run test code, check results, then clean up.
Execution Sample
Flask
def test_add_user(client, db):
    user = User(name='Alice')
    db.session.add(user)
    db.session.commit()
    assert User.query.count() == 1
This test adds a user to the test database and checks if the user count is 1.
Execution Table
StepActionDatabase StateResult
1Start test, test DB emptyUsers: 0Ready to add user
2Create User(name='Alice')Users: 0User object created, not saved
3Add user to sessionUsers: 0User staged for commit
4Commit sessionUsers: 1 (Alice)User saved in DB
5Query User countUsers: 1 (Alice)Count is 1, test passes
6Teardown test DBUsers: 0Test DB cleaned
💡 Test ends after verifying user count is 1 and cleaning test database
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
userNoneUser(name='Alice')User(name='Alice')User(name='Alice')User(name='Alice')
db.sessionEmptyEmptyHas user stagedCommittedEmpty after teardown
User.query.count()00010 after teardown
Key Moments - 3 Insights
Why does the user count stay 0 before committing?
Because adding to the session only stages changes; the commit actually saves to the database (see step 3 vs step 4 in execution_table).
Why do we use a separate test database?
To avoid changing real data and to allow tests to run safely and independently (see step 1 and step 6 in execution_table).
What happens if we forget to commit?
The user won't be saved, so the count stays 0 and the test fails (compare step 3 and step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the user count after step 3?
AUser object not created yet
B1
C0
DTest database is empty
💡 Hint
Check the 'Database State' column at step 3 in execution_table
At which step does the user get saved to the database?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the commit action in execution_table
If we skip the teardown step, what happens to the test database?
AIt stays with test data, possibly affecting other tests
BIt deletes all data automatically
CIt resets user count to 0
DNothing changes because tests are isolated
💡 Hint
See the purpose of step 6 in execution_table and key_moments about test DB cleanup
Concept Snapshot
Testing with database in Flask:
- Use a separate test database
- Create test client and setup DB
- Add and commit test data
- Query and assert results
- Teardown DB to clean
This ensures tests don't affect real data.
Full Transcript
In Flask testing with a database, we first set up a separate test database to keep tests isolated from real data. Then, we create a test client and run test functions. Inside a test, we create objects like a User, add them to the database session, and commit to save them. We then query the database to check if the data is saved correctly. After tests run, we teardown the test database to clean up. This process ensures tests are safe and repeatable without affecting real application data.