Database Tests with SQLAlchemy¶
A database test suite powered by pytest, SQLAlchemy, Alembic and Docker. It provisions a Postgres database, runs migrations, seeds test data and executes CRUD tests with detailed logging and HTML reporting, as a reference for DB-focused testing patterns built on fixtures, actions and factories.
What's Covered¶
- CRUD tests — suites for roles, priorities, statuses, tasks and users
- Schema — ORM models that match the Alembic migration schema
- Test data — seeded through factory-boy and Faker
How It's Built¶
- Postgres 15 in Docker — a
postgresservice, plus amigrateservice that applies Alembic migrations before the tests run. - Actions layer — DB action wrappers in
tests/actions/act as CRUD helpers for the tests. - Factories — factory-boy models and builders in
tests/factories/. - Fixtures —
conftest.pyprovides sessions and factories; the app uses a scoped SQLAlchemy session. - Configuration — DB host, port, user and password are loaded via
pydantic-settings. - Dual logging — a log file per run in
logs/, console output and per-test log snippets in the HTML report.
Tech Stack¶
pytest— test runnersqlalchemy— ORM models and queriesalembic— migrationspydantic/pydantic-settings— data models, validation and configurationfactory-boy+faker— data factoriespytest-html— HTML reportsuv— dependency managerruff— lint and formatdocker compose— reproducible environment
Running It¶
Install dependencies, build the images, then run migrations and tests in Docker:
make install
make build
make docker-test
make docker-test-html