Django 测试 - 我只想创建我的数据库之一 - 如何指定

标签 django django-testing django-settings

所以在我的 settings.py 中我指定了两个数据库连接(见下文)。

但是当我运行测试时,我只希望 Django 创建“默认”数据库。

有什么办法吗?我尝试添加 TEST_CREATE: False 选项,但我猜出于某种原因这仅适用于 Oracle?

我的 settings.py 片段:

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
    }

}

最佳答案

好吧,这是一种方法。我在设置我的普通数据库后将它添加到 settings.py 中。

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

关于Django 测试 - 我只想创建我的数据库之一 - 如何指定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553670/

相关文章:

python - Django 上的正则表达式不起作用

python - django app一直迁移不生效[heroku]

Django - 向管理员显示不同的模板

python - MySQL: django.db.utils.OperationalError: (1698, "Access denied for user ' root' @'localhost' ") with correct username and pw

python - 本地计算机上的默认操作系统环境

css - Django 在生产中找不到通过 CSS 文件链接的图像

django - 使用 Django 压缩机分离静态文件和资源以及使用collectstatic

python - 来自相关对象的 Django 单元测试模拟查询集

django - 如何创建用于测试的临时文件结构

django - Django 中单元测试的 auth_token