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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

What is StringIO class ? Where does it come from ?
@lbris StringIO comes from: from io import StringIO. The io module provides the Python interfaces to stream handling.
Maybe you should update your answer to include this import statement, for the sake of clarity. Thanks for your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.