Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: No module named 'notmigrations' during unit tests

I have django application 2.1.7 with django-tenant plugin (creating schemas in the database for saas).

My problem is unit testing. I run the command: python manage.py test --settings=project.settings_test and I'm getting a error: ImportError: No module named 'notmigrations'

My code in settings_test file

from .settings_base import *


class DisableMigrations(object):
    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return 'notmigrations'

MIGRATION_MODULES = DisableMigrations()
like image 987
user10605113 Avatar asked Apr 28 '19 21:04

user10605113


1 Answers

You are using an ancient hack intended for really old versions of Django (< 1.9), from before there was support to disable migrations in testing. Since you're now using a relatively recent version of Django (2.1.7), delete that code from your test settings module.

Should you want to disable migrations in tests, use the modern approach, which is putting the value to None in MIGRATION_MODULES setting.

When you supply None as a value for an app, Django will consider the app as an app without migrations regardless of an existing migrations submodule. This can be used, for example, in a test settings file to skip migrations while testing (tables will still be created for the apps’ models).

# test_settings.py
from settings import *

MIGRATION_MODULES = {
    'auth': None,
    'contenttypes': None,
    'sessions': None,
    ...
    'myapp1': None,
    'myapp2': None,
}
like image 57
wim Avatar answered Sep 22 '22 09:09

wim