Skip to main content

PyPI version Python versions License: MIT PyPI downloads

CI Coverage Status Documentation Status OpenSSF Scorecard

Code style: ruff Types: mypy uv pre-commit

See the full documentation here.

Introduction

A pytest plugin to test alembic migrations (with default tests) and which enables you to write tests specific to your migrations.

Runs on Python 3.10 – 3.15 with alembic 1.9+ and SQLAlchemy 1.4+, on Linux, macOS and Windows.

$ pip install pytest-alembic
$ pytest --test-alembic

...
::pytest_alembic/tests/model_definitions_match_ddl <- . PASSED           [ 25%]
::pytest_alembic/tests/single_head_revision <- . PASSED                  [ 50%]
::pytest_alembic/tests/up_down_consistency <- . PASSED                   [ 75%]
::pytest_alembic/tests/upgrade <- . PASSED                               [100%]

============================== 4 passed in 2.32s ===============================

The pitch

Have you ever merged a change to your models and you forgot to generate a migration?

Have you ever written a migration only to realize that it fails when there’s data in the table?

Have you ever written a perfect migration only to merge it and later find out that someone else also merged a migration and your CD is now broken!?

pytest-alembic is meant to (with a little help) solve all these problems and more. Note, due to a few different factors, there may be some minimal required setup; however most of it is boilerplate akin to the setup required for alembic itself.

Built-in Tests

  • test_single_head_revision

    Assert that there only exists one head revision.

    We’re not sure what realistic scenario involves a diverging history to be desirable. We have only seen it be the result of uncaught merge conflicts resulting in a diverged history, which lazily breaks during deployment.

  • test_upgrade

    Assert that the revision history can be run through from base to head.

  • test_model_definitions_match_ddl

    Assert that the state of the migrations matches the state of the models describing the DDL.

    In general, the set of migrations in the history should coalesce into DDL which is described by the current set of models. Therefore, a call to revision --autogenerate should always generate an empty migration (e.g. find no difference between your database (i.e. migrations history) and your models).

  • test_up_down_consistency

    Assert that all downgrades succeed.

    While downgrading may not be lossless operation data-wise, there’s a theory of database migrations that says that the revisions in existence for a database should be able to go from an entirely blank schema to the finished product, and back again.

  • Experimental tests

    • all_models_register_on_metadata

      Assert that all defined models are imported statically.

      Prevents scenarios in which the minimal import of your models in your env.py does not import all extant models, leading alembic to not autogenerate all your models, or (worse!) suggest the deletion of tables which should still exist.

    • downgrade_leaves_no_trace

      Assert that there is no difference between the state of the database pre/post downgrade.

      In essence this is a much more strict version of test_up_down_consistency, where the state of a MetaData before and after a downgrade are identical as far as alembic (autogenerate) is concerned.

    These tests will need to be enabled manually because their semantics or API are not yet guaranteed to stay the same. See the linked docs for more details!

Let us know if you have any ideas for more built-in tests which would be generally useful for most alembic histories!

Custom Tests

For more information, see the docs for custom tests (example below) or custom static data (to be inserted automatically before a given revision).

Sometimes when writing a particularly gnarly data migration, it helps to be able to practice a little timely TDD, since there’s always the potential you’ll trash your actual production data.

With pytest-alembic, you can write tests directly, in the same way that you would normally, through the use of the alembic_runner fixture.

from sqlalchemy import text


def test_gnarly_migration_xyz123(alembic_engine, alembic_runner):
    # Migrate up to, but not including this new migration
    alembic_runner.migrate_up_before('xyz123')

    # Perform some very specific data setup, because this migration is sooooo complex.
    # ...
    alembic_runner.insert_into('tablename', dict(id=1, name='foo'))

    alembic_runner.migrate_up_one()

    # `alembic_engine` is a plain sqlalchemy engine, for whenever you need to
    # inspect the result yourself.
    with alembic_engine.connect() as conn:
        rows = conn.execute(text('SELECT id FROM tablename')).fetchall()

    assert rows == [(1,)]

alembic_runner has a number of methods designed to make it convenient to change the state of your database up, down, and all around.

Installing

pip install "pytest-alembic"

Release files for pytest-alembic 0.13.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-alembic 0.13.0
File Size Uploaded
pytest_alembic-0.13.0.tar.gz 39.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-alembic 0.13.0
File Interpreter ABI Platform
pytest_alembic-0.13.0-py3-none-any.whl Python 3 none any Details

Total release size: 85.5 kB

Release files / pytest_alembic-0.13.0.tar.gz

Download URL pytest_alembic-0.13.0.tar.gz
Size 39.5 kB
Tags Source
SHA-256 checksum
How to use checksums
268ff312e14d9f9c835d0676f22fba36088e877097f7ec89a568d5ae92a5b5f4
BLAKE2b-256 checksum
How to use checksums
42be6428967095a5ab5e139b1f924f2f6d7f5ca8f69eb393f0eff0345608209c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pytest_alembic-0.13.0-py3-none-any.whl

Download URL pytest_alembic-0.13.0-py3-none-any.whl
Size 46.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98c50d394774c0fb79c1d10dcbde657ab0a3cefb90fbe406ce3dd77a166c863f
BLAKE2b-256 checksum
How to use checksums
f854aa2e6512ac86c656dbaa6e5647c6386c6227c6c1bc5b39bb932522c5d89c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.13.0 This release

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.10.6

2 release files

0.10.5

2 release files

0.10.4

2 release files

0.10.3

1 release file

0.10.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page