Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

4247 bites

Page 5

Python & FastAPI2 min read

FastAPI's TestClient: Test Your API Without a Live Server

FastAPI's TestClient simulates API requests in-memory, letting you test endpoints without a live server. Use it with pytest to verify status codes and responses. The main footgun is forgetting to pip install httpx, as it's a required dependency.

Python & FastAPI2 min read

pytest Fixtures: Reusable Test Setups

Pytest fixtures are reusable functions for test setup, like creating sample data. Your tests request them by name as arguments, and pytest automatically runs them and injects the results.

Python & FastAPI2 min read

Run One Test with Many Inputs using pytest.parametrize

Run one test function with many inputs using @pytest.mark.parametrize, avoiding repetitive code. It's ideal for checking a function against various inputs, edge cases, and expected failures. The footgun: mutable parameters like lists are passed by reference.

Python & FastAPI2 min read

Testing Async FastAPI with pytest-asyncio

To test async code, your tests must also be async. pytest-asyncio lets you write async def test_... functions to await operations like database checks after an API call.

Python & FastAPI2 min read

Testing FastAPI Lifespan Events

FastAPI lifespan events only run when TestClient is used as a context manager. Use this to test startup logic like DB pools before endpoints. Using TestClient(app) without with skips lifespan, leaving your app uninitialized and tests silently wrong.

Python & FastAPI2 min read

Mocking with Pytest's monkeypatch

Pytest's monkeypatch is a temporary stunt double for your code, safely swapping out functions or environment variables for a single test. Use it to isolate tests from network calls or filesystem access.

Testing WebSockets in FastAPI
Python & FastAPI2 min read

Testing WebSockets in FastAPI

Test a WebSocket conversation by scripting both sides. FastAPI's TestClient provides a websocket_connect context manager to send messages and assert responses sequentially, which is crucial for testing chats or live data feeds.

FastAPI: Configure API Metadata for Better Docs
Python & FastAPI2 min read

FastAPI: Configure API Metadata for Better Docs

Think of FastAPI metadata as your project's business card. It sets the title, version, and description in your auto-generated docs, making your API professional and discoverable. The main footgun is forgetting to update the version string after a release.

FastAPI: Documenting Additional API Responses
Python & FastAPI2 min read

FastAPI: Documenting Additional API Responses

Document every possible API response, not just the happy path. The responses decorator parameter lets you define alternative status codes and schemas, like a 404 error model, making your OpenAPI docs complete.

Exclude a FastAPI Endpoint from OpenAPI Docs
Python & FastAPI1 min read

Exclude a FastAPI Endpoint from OpenAPI Docs

Hide an endpoint from your API docs by setting include_in_schema=False. Use this for internal or deprecated endpoints. The footgun: this only hides the endpoint from documentation; it remains fully functional and accessible if the URL is known.

Python & FastAPI2 min read

Overriding FastAPI's OpenAPI Generator

FastAPI lets you swap app.openapi to reshape its generated schema without forking. Use this for vendor extensions, filtered operations, or merging external schemas. Forgetting to cache the result means every docs request rebuilds it and destroys performance.

Customizing FastAPI's Swagger UI Behavior
Python & FastAPI1 min read

Customizing FastAPI's Swagger UI Behavior

Treat FastAPI's Swagger UI as a configurable frontend, not a static page. You can customize its behavior by passing a dictionary of settings on app startup. This is useful for changing themes or pre-filling auth fields. The footgun: keys must be camelCase.

FastAPI Behind a Reverse Proxy: Fixing Docs URLs
Python & FastAPI2 min read

FastAPI Behind a Reverse Proxy: Fixing Docs URLs

A reverse proxy can hide the full URL from your FastAPI app, breaking OpenAPI docs. Tell your app about the proxy's path prefix by setting the root_path during initialization to ensure all generated URLs are correct.

Python & FastAPI2 min read

FastAPI Lifespan: Code Before Startup, After Shutdown

FastAPI's lifespan events are "open for business" and "closing time" routines that run once before startup and after shutdown. Use them to initialize a DB pool or load a model. The footgun is putting request-specific logic here; it runs once only.

Python & FastAPI2 min read

FastAPI's WebSocket State Machine

FastAPI manages WebSockets with a state machine, tracking client and application states separately. You must explicitly accept() a connection before communicating. This is key for chat or notification features.

Python & FastAPI2 min read

Your First Python Dockerfile Blueprint

A Dockerfile is a recipe for building a self-contained environment for your Python app. Use it to ensure your app runs identically everywhere, from your laptop to production. The common footgun is forgetting a .dockerignore file, which bloats your image.

Python & FastAPI2 min read

From Dev Server to Production: Running FastAPI with Workers

Your dev server is a single process. For production, you need a process manager to run multiple Uvicorn worker processes, handling concurrent requests and providing fault tolerance.

Mangum: Run Python ASGI Apps on Serverless
Python & FastAPI2 min read

Mangum: Run Python ASGI Apps on Serverless

Mangum is an adapter for running Python ASGI apps (like FastAPI) on serverless platforms like AWS Lambda. It translates serverless events into ASGI requests, letting you deploy existing async web apps without a rewrite.

FastAPI Container Build and Deploy Pipeline
Python & FastAPI2 min read

FastAPI Container Build and Deploy Pipeline

Treat the Docker image as the immutable artifact: one build runs everywhere. Deploy FastAPI workers behind a load balancer, one process per container. The footgun is baking secrets into the image or running multiple processes; that breaks horizontal scaling.

Python & FastAPI2 min read

FastAPI: Validating Models with Pydantic's Field

Pydantic's Field adds guardrails directly to your data model's attributes. Use it to enforce constraints like string length (max_length=50) or numeric ranges (gt=0), making your models self-validating.