Testing Web APIs (API TDD)
testModule 5: Testing Web APIs (API TDD)
Goal: Write integration tests asserting that your API routes return the expected JSON structures and status codes.
What you need to know first
Integration Testing: Testing how different components (routing, serialization, validation, database queries) work together.
FastAPI TestClient: An HTTP client tool that simulates web requests on your running FastAPI code without needing to start uvicorn. It intercepts requests inside Python, making test runs extremely fast:
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
The SOLO idea
Your test suite is your definition of correctness. When expanding endpoints or securing routes, the tests run automatically to catch regression issues immediately. The AI must fix the application logic until tests are green; never change the test values to bypass errors.
Lab 5: Endpoint Testing
Estimated AI conversations: 2-3
- Create a test file
test_main.pyin your project folder. - Step A: Plan the test setup. Open Copilot Chat and type:
Follow #file:workflows/architect.md — Plan integration tests for
main.pyusingfastapi.testclient.TestClientandpytest.- The plan must use a temporary test SQLite database
test_tasks.db. - Plan a
pytestfixture that overrides theget_dbdependency in FastAPI with a connection totest_tasks.dbusingapp.dependency_overrides[get_db]. - Plan fixtures to delete and recreate the test database schema before every test execution.
- The plan must use a temporary test SQLite database
- Step B: Critique testing isolation. In the same conversation, type:
Follow #file:workflows/critique.md — Critique the database override plan. Does it guarantee that test runs do not read or write data to the production
tasks.db? How are connections cleaned up? - Step C: Revise.
Follow #file:workflows/revise.md — Revise the test strategy to make sure dependencies are overridden correctly and test files are cleaned up after runs.
- Step D: Implement.
Follow #file:workflows/code.md — Write the test suites in
test_main.py. - Review the resulting setup. It should use the standard FastAPI override pattern:
import pytest from fastapi.testclient import TestClient from main import app from db import get_db import sqlite3 # Define database override def override_get_db(): conn = sqlite3.connect("test_tasks.db") conn.row_factory = sqlite3.Row try: yield conn finally: conn.close() app.dependency_overrides[get_db] = override_get_db client = TestClient(app) - Run your tests in the terminal:
⚠️ Pytest Command Issues: If your terminal throws apytest -vpytest: command not foundorModuleNotFoundError, your virtual environment is either not active or missing dependencies. Ensure you see(.venv)at the beginning of your terminal prompt. If you don't see it, run your activation command (e.g.source .venv/bin/activateor.venv\Scripts\Activate.ps1). If it is active but still throws errors, re-runpip install pytest fastapi httpxto ensure the testing packages are installed inside the isolated sandbox. - Ensure all tests pass. If any fail, check for database file locks or missing parameters.
- Commit:
git add test_main.py git commit -m "Module 5: integrated API tests with 100% pass rate and get_db overrides" git push
Checkpoint
Create checkpoint_05.md:
- Paste the test summary output of
pytest -v. - Explain how using a separate test database file (or transactional rollback) ensures that test runs remain reproducible and reliable.