我正在尝试设置 LiveServerTestCase。
为此,我在我的测试类中创建了一个用户
class ExampleTest(LiveServerTestCase):
user = User.objects.create_superuser('Testuser','test@user.com','1234')
user.save()
.
.
.(rest of the test)
如果没有这一行,服务器和测试就会启动,但显然它无法登录,因为之前没有创建用户。
但是有了这条线我得到了一个
django.core.exceptions.ImproperlyConfigured: settings.DATABASES
is improperly configured. Please supply the NAME value.
错误。
我是否需要在 settings.py 中为 LiveServerTestCase 设置服务器,如果是,我需要使用哪些值或在哪里找到它们?
更新:
我正在运行这个测试
python manage.py test
所以它自己设置了一个数据库,我不必在 settings.py 中定义它,或者我错了。
更新 2:
我已经定义了一个“生产”数据库(在我问这个问题之前),它看起来像这样:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'localhost', # 10.1.2.41
'NAME': 'pim_testdatabase',
'USER': 'postgres',
'PASSWORD': '1234',
'PORT': '5432',
'HAS_HSTORE': True,
'TEST':{
'NAME': 'test_pim_testdatabase'
},
},
}
仍然出现异常。
请您参考如下方法:
您需要在 DATABASES 中设置数据库设置。
Django sets up a test database corresponding to every database that is defined in the
DATABASESdefinition in your settings file.By default the test databases get their names by prepending
test_to the value of theNAMEsettings for the databases defined inDATABASES.
如果您想使用不同的数据库名称,请在 DATABASES 中的任何给定数据库的 TEST 字典中指定 NAME。
示例测试数据库配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'USER': 'mydatabaseuser',
'NAME': 'mydatabase',
'TEST': { # test database settings
'NAME': 'mytestdatabase', # test database name
},
},
}
