pytest-alembic
A pytest plugin for verifying alembic migrations.
What it is and what it does
pytest-alembic is a pytest plugin that automates testing of alembic database migrations. It provides four built-in tests that catch common migration mistakes: ensuring a single head revision exists (no diverged history), verifying the full upgrade path works, confirming migrations match your SQLAlchemy model definitions, and validating that downgrades succeed. Beyond these defaults, it offers fixtures like `alembic_runner` to write custom tests for complex data migrations, letting you practice test-driven development on tricky migration logic before running against production.
The plugin sits between pytest and your alembic environment, running migrations in a test database and asserting their correctness. It solves real problems: catching forgotten migrations before merge, detecting migration failures with existing data, and preventing broken deployments from conflicting migrations merged in parallel. Most projects need minimal setup beyond what alembic itself requires.
Use it for:
- Catch forgotten migrations in CI before they reach production by asserting model definitions match the migration history.
- Test complex data migrations with custom logic using the alembic_runner fixture to set up state and verify outcomes.
- Detect diverged migration histories from merge conflicts automatically rather than discovering them during deployment.
- Validate that all migrations can be reversed cleanly, ensuring your database can roll back if needed.
- Ensure all SQLAlchemy models are properly imported and registered so autogenerate captures the full schema.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that runs built-in and custom tests against alembic database migrations to catch common issues like missing migrations, merge conflicts, and data-loss bugs.
Yes. This package solves a real class of production bugs (broken migrations, merge conflicts, forgotten migrations) with low install friction and active maintenance. If you use alembic and SQLAlchemy, the built-in tests alone provide immediate value in CI. The custom test fixtures add flexibility for complex migrations. No known vulnerabilities, permissive license, and steady releases since 2020 make it a safe, practical choice.
Install
pytest-alembic on PyPI
pip
pip install pytest-alembicuv
uv add pytest-alembicpoetry
poetry add pytest-alembicInstalling pytest-alembic
Before you install
Low friction: pure Python wheel with three well-established runtime dependencies (pytest, alembic, sqlalchemy). Active maintenance with a recent commit on 2026-07-27 and steady release history since 2020-04-02.
License in practice
MIT license is permissive; you can use this package freely in commercial and open-source projects with minimal restrictions.
Quickstart
pip install pytest-alembic
# In your project with alembic migrations configured:
pytest --test-alembic
# Or write a custom test:
from pytest_alembic import alembic_runner
def test_my_migration(alembic_runner):
alembic_runner.migrate_up_before('revision_id')
# ... setup data ...
alembic_runner.migrate_up_one()
Requires an existing alembic migration environment configured in your project; pytest-alembic integrates with it rather than setting it up from scratch.
Verify before relying
- Whether the built-in tests (single_head_revision, upgrade, model_definitions_match_ddl, up_down_consistency) run automatically or require explicit configuration.
- Exact setup boilerplate required beyond standard alembic initialization.
- Whether experimental tests (all_models_register_on_metadata, downgrade_leaves_no_trace) are stable enough for production use.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (<4,>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 3 — pytest, alembic, sqlalchemy |
| Maintenance | actively maintained — 444 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,284,731/month — #4,111 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_alembic-0.12.1-py3-none-any.whl
Keywords: pytest, sqlalchemy, alembic, migration, revision
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
alembicAlembic generates and manages database schema…
permissive · top 1,000 on PyPI
django-test-migrationsTests Django schema and data migrations,…
permissive · top 15,000 on PyPI
Flask-AlembicFlask-Alembic integrates Alembic database…
permissive · top 15,000 on PyPI
alembic-autogenerate-enumsExtends Alembic's autogenerate to detect and…
permissive · top 15,000 on PyPI
alembic_utilsExtends Alembic to autogenerate database…
permissive · top 5,000 on PyPI
alembic-postgresql-enumExtends Alembic to automatically detect and…
permissive · top 5,000 on PyPI
aerichAerich is a database migration tool for…
permissive · top 15,000 on PyPI
Flask-MigrateFlask-Migrate handles SQLAlchemy database…
permissive · top 5,000 on PyPI
pytest-mysqlA pytest plugin that provides MySQL and MariaDB…
copyleft · top 15,000 on PyPI
starrocksProvides a SQLAlchemy dialect and Alembic…
permissive · top 15,000 on PyPI