
Testing FastAPI: Unit and Integration Tests
Verify your code before you ship. Learn how to use TestClient and Pytest to write reliable, automated tests for your FastAPI endpoints.
Testing FastAPI: Unit and Integration Tests
A professional developer doesn't "Check" their API by clicking buttons in Swagger. They write Automated Tests that check their code hundreds of times per day.
In this lesson, we learn how to use Pytest and the TestClient to ensure our API never breaks.
1. The TestClient
FastAPI (via Starlette) provides a TestClient. It behaves like a browser or Postman, but it runs entirely in your computer's memory—making it incredibly fast.
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/")
def read_main():
return {"msg": "Hello World"}
# Initialize the client
client = TestClient(app)
def test_read_main():
# Make a request
response = client.get("/")
# Assert (Verify) the result
assert response.status_code == 200
assert response.json() == {"msg": "Hello World"}
2. Using Pytest
Pytest is the standard testing engine for Python. You create files named test_*.py, and Pytest automatically finds and runs all your test functions.
Running Tests:
pytest
3. Integration Testing: Checking the Database
A Unit Test checks a single function in isolation. An Integration Test checks if your API talks correctly to other systems (like your database).
Pro Tip: Always use a separate "Test Database" (usually an in-memory SQLite database) so your tests don't delete your real data!
4. Testing Payloads and Headers
You can test complex scenarios like sending JSON bodies or checking if a specific header exists.
def test_create_user():
response = client.post(
"/users/",
json={"username": "shshell", "email": "dev@shshell.com"},
)
assert response.status_code == 201
assert "id" in response.json()
Visualizing the Test Loop
graph TD
A["Write Code"] --> B["Run Pytest"]
B --> C{Tests Pass?}
C -- "No" --> D["Fix Bugs"]
D --> B
C -- "Yes" --> E["Commit to Git"]
E --> F["CI/CD: Automated Testing in Cloud"]
Summary
- TestClient: Your "Virtual Browser" for internal requests.
- Pytest: The tool that manages and executes your tests.
- Assert: The keyword that defines a "Pass" or "Fail."
- Speed: Automated tests allow you to refactor your code with confidence.
In the next lesson, we’ll look at Mocking and Dependency Overrides, ensuring our tests stay isolated and reliable.
Exercise: The Status Checker
Write a test for an endpoint @app.get("/health").
The endpoint should return {"status": "up"} and a 200 status code. Write the full test_health_check function using client.get().