不同的数据库在Django中进行测试?

Nic*_*ner 53 sqlite django postgresql unit-testing

DATABASES = {
#    'default': {
#        'ENGINE': 'postgresql_psycopg2',
#        ...
#    }

    # for unit tests
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase'
    }
}
Run Code Online (Sandbox Code Playgroud)

我有两个数据库:一个我想用于单元测试,一个用于其他一切.是否可以在Django 1.2.4中配置它?

(我问的原因是因为使用postgresql我收到以下错误:

foo@bar:~/path/$ python manage.py test
Creating test database 'default'...
Got an error creating the test database: permission denied to create database

Type 'yes' if you would like to try deleting the test database 'test_baz', or 'no' to cancel: yes
Destroying old test database...
Got an error recreating the test database: database "test_baz" does not exist
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误?我想我真的不在乎我是否总能使用SQLite进行单元测试,因为这样可以正常工作.)

Sam*_*lan 69

在你的settings.py(或local_settings.py)中:

import sys
if 'test' in sys.argv:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase'
    }
Run Code Online (Sandbox Code Playgroud)

  • @sleepycal为什么会这样? (14认同)
  • 这根本就是错误的,请不要 (8认同)
  • @Rosarch ......是的; 它有点"hacky",但你是对的!有用.:)但是,恕我直言,必须拥有*UNIT*测试的任何数据库是hackish.我希望Django在这方面有不同的理念. (6认同)

Geo*_*ing 34

我处理这个问题的方法是通过拥有多个设置文件,因为我使用它来维护一组通用设置,并对每个实例进行修改.设置比其他一些解决方案要复杂一些,但我还是需要这样做,因为我正在为本地开发,远程开发,升级和生产管理略有不同的设置.

https://code.djangoproject.com/wiki/SplitSettings有许多用于管理设置的选项,我选择了类似于https://code.djangoproject.com/wiki/SplitSettings#SimplePackageOrganizationforEnvironments中描述的做法.

所以,在我的Django项目目录中,我有一个如下所示的设置文件夹:

$ tree settings
settings
??? defaults.py
??? dev.py
??? dev.pyc
??? __init__.py
??? lettuce.py
??? travis.py
??? unittest.py
Run Code Online (Sandbox Code Playgroud)

常见设置在settings/defaults.py中,我在实例设置文件中导入这些设置.所以settings/unittest.py看起来像这样:

from defaults import *

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'my_database',
    }
} 
Run Code Online (Sandbox Code Playgroud)

然后,当我想运行测试时,我只执行:

$ ./manage.py test --settings=settings.unittest
Run Code Online (Sandbox Code Playgroud)

使用sqlite进行测试.如果我想使用不同的测试运行器或数据库配置,我将使用不同的设置模块.


Leo*_*Mak 17

想提一下你可以指定测试数据库了settings.py.请参阅 https://docs.djangoproject.com/en/2.0/ref/settings/#test:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'mydatabaseuser',
        'NAME': 'mydatabase',
        'TEST': {
            'NAME': 'mytestdatabase',
        },
    },
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,你不能改变`HOST`。 (2认同)

laf*_*ste 7

这大大加速了测试执行.

import sys

if 'test' in sys.argv:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'TEST_CHARSET': 'UTF8', # if your normal db is utf8
        'NAME': ':memory:', # in memory
        'TEST_NAME': ':memory:', # in memory
    }

    DEBUG = False # might accelerate a bit
    TEMPLATE_DEBUG = False

    from django.core.management import call_command
    call_command('syncdb', migrate=True) # tables don't get created automatically for me
Run Code Online (Sandbox Code Playgroud)


Cac*_*aco 6

我解决了这个问题,只需创建其他设置常量DATABASES_AVAILABLE

DATABASES_AVAILABLE = {
    'main': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'nep',
        'USER': 'user',
        'PASSWORD': 'passwd',
        'HOST': 'localhost',
    },
    'remote': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'nes_dev',
        'USER': 'usr',
        'PASSWORD': 'passwd',
        'HOST': '200.144.254.136',
    },
    'sqlite': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}

# This solves the problem with tests
# Define a system variable called DJANGO_DATABASE_TEST and set it to the
# the database you want
database = os.environ.get('DJANGO_DATABASE_TEST', 'main')
DATABASES = {
    'default': DATABASES_AVAILABLE[database]
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ero 5

虽然这个问题已经解决了...

如果您的测试数据库只是一个普通的数据库:

我认为您没有进行单元测试,因为您依赖于数据库。不管怎样,django 包含一个测试类型(不是单一的):django.test.TestCase

您需要从中派生django.test.TestCase,而不是unittest.TestCase为您创建一个新的 rehershal 数据库,该数据库将在测试结束时被销毁。

在以下链接测试 Django 应用程序中有关于使用 db 进行测试的有趣解释/提示