Python & FastAPI•Part 1: Candid Experience, Architecture & Intro (1 - 5)
Approach to automated testing in Python:
In Simple Words:
pytestwithpytest-asynciofor async tests.- Wrap tests in transactional rollbacks so every test runs in an isolated nested transaction that rolls back on teardown (fast, clean, and avoids database truncation).
- Mock external HTTP APIs using
respxand run real PostgreSQL/Redis instances in CI with Testcontainers.
python
import pytest
from httpx import AsyncClient, ASGITransport
from app.main import app
@pytest.mark.asyncio
async def test_create_user(db_session):
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.post("/api/v1/users", json={"email": "test@example.com", "full_name": "Test User", "age": 28})
assert response.status_code == 201
assert response.json()["user"]["email"] == "test@example.com"