← Flask learning path
Flask · Module 9 of 10

Testing Flask apps

Write tests with pytest and the Flask test client.

📖 Key ideas

  1. pytest is the common test runner.
  2. app.test_client() sends fake requests.
  3. Use fixtures to create the app and a test database.
  4. Assert status codes and JSON: assert res.status_code == 200.
  5. Run tests in CI on every push.
💡 Real-life example: The test client is a robot customer that visits every page and reports problems.
pytestPython test framework
test_clientSimulates HTTP requests
FixtureReusable test setup
TESTINGConfig flag for test mode