Skip to content

Instantly share code, notes, and snippets.

@janlis-ff
Created December 20, 2023 14:11
Show Gist options
  • Save janlis-ff/df630ce46acc99bc2ee6733b82865a8a to your computer and use it in GitHub Desktop.
Save janlis-ff/df630ce46acc99bc2ee6733b82865a8a to your computer and use it in GitHub Desktop.
A pytest script for Django projects, ensuring all model changes are captured in migrations by failing the test if any migrations are missing.
from io import StringIO
import pytest
from django.core.management import call_command
@pytest.mark.django_db
class TestMissingMigrations:
error_message = (
"\n\tMissing migrations detected. Please "
"run `python manage.py makemigrations`"
)
def test_migrations(self):
# GIVEN
output = StringIO()
# WHEN
try:
call_command("makemigrations", "--dry-run", stdout=output)
except Exception:
pytest.fail("Unable to determine missing migrations")
value = output.getvalue()
# THEN
assert value == "No changes detected\n", self.error_message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment