0

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?

MrGlass
  • 9,094
  • 17
  • 64
  • 89

1 Answers1

2

You can add the following unittest:

from io import StringIO

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.

Vincent
  • 1,494
  • 12
  • 26
  • What is StringIO class ? Where does it come from ? – lbris Jan 13 '23 at 20:44
  • @lbris StringIO comes from: `from io import StringIO`. The io module provides the Python interfaces to stream handling. – Vincent Jan 16 '23 at 11:01
  • 1
    Maybe you should update your answer to include this import statement, for the sake of clarity. Thanks for your answer. – lbris Jan 17 '23 at 12:40