Disable migrations when running unit tests in Django 1.7 Disable migrations when running unit tests in Django 1.7 django django

Disable migrations when running unit tests in Django 1.7


Look at this workaround, posted by Bernie Sumption to the Django developers mailing list:

If makemigrations has not yet been run, the "migrate" command treats an app as unmigrated, and creates tables directly from the models just like syncdb did in 1.6. I defined a new settings module just for unit tests called "settings_test.py", which imports * from the main settings module and adds this line:

MIGRATION_MODULES = {"myapp": "myapp.migrations_not_used_in_tests"}

Then I run tests like this:

DJANGO_SETTINGS_MODULE="myapp.settings_test" python manage.py test

This fools migrate into thinking that the app is unmigrated, and so every time a test database is created it reflects the current structure of models.py.

In Django 1.9, this situation is improved somewhat, and you can set the value to None:

MIGRATION_MODULES = {"myapp": None}


Here is the end of my settings file :

class DisableMigrations(object):    def __contains__(self, item):        return True    def __getitem__(self, item):        return NoneTESTS_IN_PROGRESS = Falseif 'test' in sys.argv[1:] or 'jenkins' in sys.argv[1:]:    logging.disable(logging.CRITICAL)    PASSWORD_HASHERS = (        'django.contrib.auth.hashers.MD5PasswordHasher',    )    DEBUG = False    TEMPLATE_DEBUG = False    TESTS_IN_PROGRESS = True    MIGRATION_MODULES = DisableMigrations()

based on this snippet

I disabled migrations only when tests are running


django-test-without-migrations adds a --nomigrations flag to manage.py test. Works like a charm.