用于在 Django 中进行测试的不同数据库?
DATABASES = {
# 'default': {
# 'ENGINE': 'postgresql_psycopg2',
# ...
# }
# for unit tests
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
}
}
我有两个数据库:一个用于单元测试,另一个用于其他所有内容.是否可以在 Django 1.2.4 中进行配置?
I have two databases: one I'd like to use for unit tests, and one for everything else. Is it possible to configure this in Django 1.2.4?
(我问的原因是因为使用 postgresql 我收到以下错误:
(The reason I ask is because with postgresql I'm getting the following error:
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
为什么我会收到这个错误?我想我并不在乎我是否总是可以使用 SQLite 进行单元测试,因为它可以正常工作.)
Why could I be getting this error? I guess I don't really care if I can always use SQLite for unit tests, as that works fine.)
推荐答案
在你的settings.py
(或local_settings.py
)中:
import sys
if 'test' in sys.argv:
DATABASES['default'] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
}
相关文章