Django Testing - I only want one of my databases created - how to specify

久未见 提交于 2019-12-12 14:43:08

问题


So in my settings.py I specified two database connections (see below).

But when I run tests I only want Django to create the 'default' database.

Is there any way to do this? I tried adding the TEST_CREATE: False option but I guess that's only for Oracle for some reason?

My settings.py snippet:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'App1',                      # Or path to database file if using sqlite3.
        'USER': 'webaccess',                      # Not used with sqlite3.
        'PASSWORD': '***',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    },
    'default_root': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'App1',                      # Or path to database file if using sqlite3.
        'USER': 'django',                      # Not used with sqlite3.
        'PASSWORD': '****',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        'TEST_CREATE': False            #Don't create when testing
    }

}

回答1:


Well, here's one way to do it. I added this to settings.py after setting up my normal databases.

if 'test' in sys.argv:
    DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3',}}


来源:https://stackoverflow.com/questions/10553670/django-testing-i-only-want-one-of-my-databases-created-how-to-specify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!