litestar-testing
SKILL.md
Testing
Execution Workflow
- Choose the right client shape first:
TestClient,AsyncTestClient,create_test_client, or subprocess live-server helpers. - Build fixtures with deterministic configuration, dependencies, and lifecycle boundaries.
- Isolate external I/O with injected fakes or mocked dependencies instead of monkeypatching internals.
- Assert full contracts: status code, body, headers, cookies, side effects, and failure payload shape.
- Cover error paths deliberately, including validation failures, app-level exception mappings, event-listener failures, layered override precedence, and schema/docs regressions.
Core Rules
- Keep tests deterministic in time, randomness, and I/O.
- Use
AsyncTestClientwhen tests, fixtures, and app resources must share the same event loop. - Use
create_test_clientfor isolated handler tests or small app subsets. - Prefer dependency injection and fake services over patching transport-layer code.
- Assert stable error contracts, not just that an exception occurred.
- Reset overrides and fixture state between tests.
- Use live-server subprocess helpers when the in-process client cannot emulate the transport correctly.
Decision Guide
- Use
TestClientfor synchronous tests that do not need to share async resources with the app. - Use
AsyncTestClientwhen async fixtures or resources are involved. - Use
create_test_clientwhen you want a disposable app assembled inline for one test. - Use
websocket_connect()for websocket contract tests. - Use subprocess clients for infinite SSE streams or other cases where HTTPX's in-process transport is insufficient.
Reference Files
Read only the sections you need:
- For client selection, fixtures, async event-loop behavior, websocket tests, blocking-portal usage, and subprocess live-server helpers, read references/client-patterns.md.
- For dependency overrides, mocked dependencies, exception-contract assertions, event-emission tests, listener-failure tests, schema/docs regressions,
404/405testing, and layered precedence tests, read references/failure-patterns.md.
Recommended Defaults
- Turn on
app.debugonly when a test needs it; otherwise keep tests aligned with production-facing behavior. - Keep app assembly near the test when only one handler or one contract is under test.
- Assert response payloads and headers fully for custom exception handlers.
- Use one fake implementation per dependency boundary instead of broad monkeypatching.
- Prefer explicit fixtures over hidden global state.
Anti-Patterns
- Using
TestClientwith async fixtures that create loop-bound resources. - Asserting only status codes for custom error handlers.
- Letting dependency overrides leak across tests.
- Treating websocket tests like plain HTTP tests and ignoring handshake or frame behavior.
- Using in-process clients for transport patterns the docs call out as poor fits, such as infinite SSE streams.
- Relying on debug-only stack traces as part of the tested contract.
Validation Checklist
- Confirm client choice matches the event-loop and resource model.
- Confirm startup and shutdown behavior are exercised where relevant.
- Confirm dependency overrides and mocked services are scoped to the test.
- Confirm expected failures produce stable payloads, headers, and status codes.
- Confirm emitted events and listener side effects are asserted when the feature depends on the event bus.
- Confirm app-level
404and405handlers are tested at app scope. - Confirm layered overrides behave as expected at app, router, controller, and handler scope.
- Confirm websocket tests assert both send and receive behavior.
- Confirm live-server helpers are used for scenarios the in-process client cannot model correctly.
- Confirm generated schema/docs regressions are covered when response or error contracts change.
Cross-Skill Handoffs
- Use
litestar-dependency-injectionto design override-friendly providers and scope rules. - Use
litestar-exception-handlingto standardize failure envelopes before locking tests. - Use
litestar-eventswhen tests depend on event IDs, listeners, and custom emitter behavior. - Use
litestar-responsesfor stream, redirect, file, and SSE contract details. - Use
litestar-openapiwhen tests must lock down schema or docs behavior. - Use
litestar-websocketswhen the main challenge is websocket endpoint design rather than test harness setup.
Litestar References
Weekly Installs
12
Repository
alti3/litestar-skillsGitHub Stars
5
First Seen
13 days ago
Security Audits
Installed on
opencode12
gemini-cli12
github-copilot12
codex12
kimi-cli12
cursor12