advanced-alchemy
Auto-activate for advanced_alchemy imports, alembic/, SQLAlchemyAsyncRepositoryService, SQLAlchemyAsyncConfig, repository_type, service_class, filters, or…
Auto-activate for test_*.py, conftest.py, litestar.testing, TestClient, AsyncTestClient, create_test_client, create_async_test_client, anyio, Guard mocks, DI overrides, or handler tests. Not for generic pytest.
$ npx -y skills add litestar-org/litestar-skills --skill litestar-testing --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/litestar-testingContext preview
The summary Claude sees to decide when to auto-load this skill.
Auto-activate for test_*.py, conftest.py, litestar.testing, TestClient, AsyncTestClient, create_test_client, create_async_test_client, anyio, Guard mocks, DI overrides, or handler tests. Not for generic pytest.
name: litestar-testing description: "Auto-activate for test_*.py, conftest.py, litestar.testing, TestClient, AsyncTestClient, create_test_client, create_async_test_client, anyio, Guard mocks, DI overrides, or handler tests. Not for generic pytest."
Litestar-specific testing patterns built on pytest + anyio. Covers:
For JS-side testing (Vitest, Testing Library, Playwright), use the upstream Vitest docs and Litestar's own JS examples. Out of scope here.
| Client | When to Use | Lifespan | Internals | | --- | --- | --- | --- | | `TestClient` | Sync test bodies, simple smoke tests | Triggered via context manager | Runs ASGI in a thread pool | | `AsyncTestClient` | **Default for new tests** — async test bodies, lifespan-aware fixtures | Native async lifespan | Runs ASGI in the test event loop |
# AsyncTestClient — preferred
from litestar.testing import AsyncTestClient
async def test_index(async_client: AsyncTestClient):
resp = await async_client.get("/")
assert resp.status_code == 200# TestClient — legacy / sync
from litestar.testing import TestClient
def test_index(client: TestClient):
resp = client.get("/")
assert resp.status_code == 200# conftest.py
import pytest
@pytest.fixture
def anyio_backend() -> str:
return "asyncio"# tests/test_x.py import pytest @pytest.mark.anyio async def test_something(): ...
Litestar's runtime is anyio-based; do not use `pytest-asyncio` — it conflicts.
# conftest.py
from collections.abc import AsyncGenerator
import pytest
from litestar import Litestar
from litestar.testing import AsyncTestClient
from app import create_app
@pytest.fixture
async def app() -> Litestar:
return create_app()
@pytest.fixture
async def async_client(app: Litestar) -> AsyncGenerator[AsyncTestClient, None]:
async with AsyncTestClient(app=app) as client:
yield client`async with AsyncTestClient(...)` runs `on_startup` / `on_shutdown` hooks and plugin lifespans (Vite, SAQ, SQLAlchemy session pool, etc.). Without the context manager, lifespan does not fire.
Guards are functions of `(connection, route_handler) -> None`. Test the real guard with fake identity or authorization providers. Build a fresh app with replacement providers; Litestar has no mutable `app.dependency_overrides` registry.
from litestar.di import Provide
@pytest.fixture
async def async_client() -> AsyncGenerator[AsyncTestClient, None]:
fake_users_service = FakeUserService()
async def provide_fake_users_service() -> UserService:
return fake_users_service
test_app = create_app(
dependencies={
"users_service": Provide(provide_fake_users_service),
},
)
async with AsyncTestClient(app=test_app) as client:
yield clientfrom collections.abc import AsyncGenerator
from unittest.mock import AsyncMock
import pytest
from litestar.di import Provide
from litestar.testing import AsyncTestClient
@pytest.fixture
async def async_client() -> AsyncGenerator[tuple[AsyncTestClient, AsyncMock], None]:
fake_email = AsyncMock()
async def provide_fake_email() -> AsyncMock:
return fake_email
app = create_app(
dependencies={
"email_service": Provide(provide_fake_email),
},
)
async with AsyncTestClient(app=app) as client:
yield client, fake_emailFor isolated handler tests, pass replacements directly to `create_async_test_client(..., dependencies={...})`. Do not mutate a constructed app; rebuilding preserves dependency resolution and prevents parallel tests from sharing overrides.
Combine `pytest-databases` fixtures with the app fixture. See `../pytest-databases/SKILL.md`.
# conftest.py
pytest_plugins = ["pytest_databases.docker.postgres"]
@pytest.fixture
async def app(postgres_service) -> Litestar:
from app import create_app
from app.config import Settings
settings = Settings(
database_url=f"postgresql+asyncpg://{postgres_service.user}:{postgres_service.password}@{postgres_service.host}:{postgres_service.port}/{postgres_service.database}"
)
return create_app(settings=settings)The `postgres_service` fixture starts a Postgres container. Inject its connection details into the app config.
| Body Type | Pass via | | --- | --- | | JSON | `client.post("/", json={...})` | | Form | `client.post("/", data={...})` | | Multipart (file upload) | `client.post("/", files={"file": ("name.txt", b"content", "text/plain")})` | | Raw bytes | `client.post("/", content=b"...")` | | Custom content-type | `client.post("/", content=b"...", headers={"Content-Type": "..."})` |
async def test_create_user(async_client):
rOpinionated, first-party agent skills, plugins, subagents, slash commands, and MCP servers for the Litestar framework and its ecosystem — publishable to every major AI agent and IDE from a single repo.
Repo: litestar-org/litestar-skills
Auto-activate for advanced_alchemy imports, alembic/, SQLAlchemyAsyncRepositoryService, SQLAlchemyAsyncConfig, repository_type, service_class, filters, or…
Auto-activate for Google ADK, LlmAgent, Runner, SQLSpecSessionService, Vertex AI, SSE agent chats, tool calls, or Litestar model workflows. Not for offline ML…
Auto-activate for guards=, Guard, ASGIConnection, JWTAuth, JWTCookieAuth, SessionAuth, role or tenant checks, or WebSocket auth. Not for frontend route…
Auto-activate for litestar_autowire, AutowirePlugin, AutowireConfig, domain_packages, AutowireIntegration, AutowireLoader, or clear_autowire_cache. Not for…
Auto-activate for uv build, hatch build, PyApp, PYAPP_*, wheel assets, GitHub release matrices, cargo-zigbuild, or python-build-standalone. Not for runtime…
Auto-activate for SQLAlchemyAsyncRepositoryService, SQLSpecAsyncService, create_filter_dependencies, LimitOffsetFilter, OffsetPagination, filters, or CRUD…