用于在 Django 中进行测试的不同数据库? [英] Different db for testing in Django?

查看:30
本文介绍了用于在 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'
    }

这篇关于用于在 Django 中进行测试的不同数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆