Disable migrations when running unit tests in Django 1.7

后端 未结 7 1231
一向
一向 2020-11-30 19:57

Django 1.7 introduced database migrations.

When running the unit tests in Django 1.7, it forces a migrate, that takes a long time. So I woul

7条回答
  •  时光说笑
    2020-11-30 20:42

    For django 1.9 and up the answer of Guillaume Vincent does not work anymore, so here's a new solution:

    I'm using this snippet in my settings file, after the definition of the INSTALLED_APPS

    if os.environ.get('TESTS_WITHOUT_MIGRATIONS', False):
        MIGRATION_MODULES = {
            app.split('.')[-1]: None for app in INSTALLED_APPS
        }
    

    It iterates over all installed apps and marks each as having no migration module. See the django docs for more information.

    Using this snippet you can run your tests, setting the environment variable TESTS_WITHOUT_MIGRATIONS, e.g.:

    TESTS_WITHOUT_MIGRATIONS=1 ./manage.py test
    

提交回复
热议问题