skillfed

pytest-alembic

A pytest plugin for verifying alembic migrations.

pytest-alembic v0.12.1 1.3M downloads/30d#4,111 on PyPI250
Permissive license MIT Active released

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-alembic

uv

uv add pytest-alembic

poetry

poetry add pytest-alembic

Installing 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

Framework :: PytestLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.9

Tags

test alembic migrationspytest database migration testingalembic migration validationsqlalchemy migration testingdatabase schema migration testsalembic upgrade downgrade testsmigration conflict detection
database-migrationssqlalchemyci-testing

More Testing packages