Add pytest fixtures (engine, db_session, client) with session-scoped event loop to avoid asyncpg loop mismatch errors. Smoke tests verify all three main API endpoints return empty results on a clean DB. Test DB provided by docker-compose.test.yml on port 5433. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1.8 KiB
1.8 KiB
title, status, type, priority, created_at, updated_at, parent, blocking
| title | status | type | priority | created_at | updated_at | parent | blocking | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Set up backend test infrastructure | completed | task | normal | 2026-02-10T09:32:57Z | 2026-02-21T11:33:32Z | nuzlocke-tracker-yzpb |
|
Set up the foundational test infrastructure for the FastAPI backend so that all subsequent test tasks can build on it.
Approach
- Session-scoped async engine: creates all tables once via
Base.metadata.create_all(), drops them after all tests finish - Function-scoped
db_sessionfixture: provides a freshAsyncSession, overrides theget_sessionFastAPI dependency, and truncates all tables after each test for isolation - Function-scoped
clientfixture:httpx.AsyncClientwithASGITransport— hits the real app stack including middleware and routing asyncio_default_fixture_loop_scope = "session"andasyncio_default_test_loop_scope = "session"added to pyproject.toml so all fixtures and tests share the same session event loop (required to avoid asyncpg "Future attached to different loop" errors)- Test database URL read from
TEST_DATABASE_URLenv var (default:postgresql+asyncpg://postgres:postgres@localhost:5433/nuzlocke_test) - The test DB is provided by
docker-compose.test.yml(postgres on port 5433,nuzlocke_testDB created automatically)
Checklist
- Add
asyncio_default_fixture_loop_scope = "session"andasyncio_default_test_loop_scope = "session"topyproject.toml - Create
backend/tests/conftest.pywithengine,db_session, andclientfixtures - Write a smoke test in
backend/tests/test_smoke.pyto verify the setup - Verify all tests pass (
pytestfrom backend dir)