Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python LiveServerTestCase-设置。数据库配置不正确_Python_Django_Python 3.x_Selenium_Testing - Fatal编程技术网

Python LiveServerTestCase-设置。数据库配置不正确

Python LiveServerTestCase-设置。数据库配置不正确,python,django,python-3.x,selenium,testing,Python,Django,Python 3.x,Selenium,Testing,我正在尝试设置LiveServerTestCase 为此,我在Testclass中使用 class ExampleTest(LiveServerTestCase): user = User.objects.create_superuser('Testuser','test@user.com','1234') user.save() . . .(rest of the test) 如果没有这一行,服务器和测试将启动,但显然它无法登录,因为之前没有创建用户 但有了这条线,我得到了一

我正在尝试设置LiveServerTestCase

为此,我在Testclass中使用

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'
      },
  },
}

仍然会出现异常。

您需要在设置中设置数据库

Django为每个数据库设置一个对应的 在设置文件的
数据库
定义中定义

默认情况下,测试数据库通过在
test\uu
前面加上前缀来获取其名称 在
数据库
中定义的数据库的
名称
设置值

如果要使用不同的数据库名称,请在
测试
字典中为
数据库
中的任何给定数据库指定
名称

测试数据库配置示例:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'mydatabaseuser',
        'NAME': 'mydatabase',
        'TEST': { # test database settings
            'NAME': 'mytestdatabase', # test database name
        },
    },
}

问题是您正在类定义中创建用户。在创建数据库之前加载测试类时运行

class ExampleTest(LiveServerTestCase):
    user = User.objects.create_superuser('Testuser','test@user.com','1234')
    user.save()  # This save isn't required -- it has been saved already
class ExampleTest(LiveServerTestCase):
    def test_user(self):
        self.user = User.objects.create_superuser('Testuser','test@user.com','1234')
        ...
您可以通过将用户创建移动到单个测试中来解决此问题。然后,在创建数据库之后,当测试方法运行时,将创建用户

class ExampleTest(LiveServerTestCase):
    user = User.objects.create_superuser('Testuser','test@user.com','1234')
    user.save()  # This save isn't required -- it has been saved already
class ExampleTest(LiveServerTestCase):
    def test_user(self):
        self.user = User.objects.create_superuser('Testuser','test@user.com','1234')
        ...
Django1.8有一种方法,您可以为整个测试用例设置一次初始数据。这样做更快,重复性也更少

class ExampleTest(LiveServerTestCase):
    @classmethod
    def setUpTestData(cls):
        # Set up data for the whole TestCase
        self.user = User.objects.create_superuser('Testuser','test@user.com','1234')

    def test_user(self):
        # access the user with self.user
        ...
在Django的早期版本中,没有
setUpTestData
,您可以在方法中创建用户


忘了加上我用python manage.py test运行测试,因为它正在为自己设置一个testdatabase,所以我不必在我的设置中定义一个数据库,或者我需要吗?将更新问题。是的,您需要在
数据库
设置中定义一个数据库。我第二次更新了问题,请查看。如果是名为
pim_testdatabase
的默认数据库,Django将自动创建一个名为
test\u
+
pim\u testdatabase
的测试数据库,即
test\u pim\u testdatabase
。检查
DEFAULT
中定义的数据库设置是否正确,是否正在建立连接。另外,请确保用户定义的用户具有创建测试数据库的权限。我认为您的关注点是错误的,或者我无法理解您的意思。正如我在问题中所写的那样,只要没有使用
create\u superuser
的行,测试就会毫无问题地开始运行,并启动服务器和数据库。我想,我的问题在于create_SuperUserAlt,虽然我自己发现了这一点,只是想把它作为一个答案添加进来,谢谢阿拉斯代尔,你昨天和今天都回答了我的问题。非常感谢你。