Home > Software engineering >  Django test to check for pending migrations
Django test to check for pending migrations

Time:09-07

Django has a --check argument that let's you check if migrations need to be created for your project. This is relatively easy to add in CI, but that won't perform the check on developer computers.

I want to add this check as a unit test in my Django project, so that it is covered when developers run our test suite.

What is a good way to accomplish that?

CodePudding user response:

You can add the following unittest:

def test_for_missing_migrations():
    output = StringIO()
    call_command("makemigrations", no_input=True, dry_run=True, stdout=output)
    assert output.getvalue().strip() == "No changes detected", (
        "There are missing migrations:\n %s" % output.getvalue()
    )

It checks whether the result of python manage.py makemigrations returns a "No changes detected", which means there are no migrations that need to be created.

  • Related