Created
December 20, 2023 14:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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