首页 > 解决方案 > 允许在 Django 测试中登录应用程序

问题描述

我正在将测试用例写入我的应用程序。请在下面找到tests.py的代码片段

class TestGetDetailsPositive(SimpleTestCase):
    def setUp(self):
      self.credentials = {
        'username': '****',
        'password': '****'
      }

@responses.activate
def test_get_details(self):
    self.client.login(username='****', password='****')
    response = self.client.get('/accounts/selectdetails/')
    self.assertContains(response, 'National', status_code=200)
    self.assertTemplateUsed(response, 'accounts/select-details.html')

def test_view_url_exists_at_desired_location(self):
    url = reverse('selectdetails')
    self.assertEquals(resolve(url).func.view_class, SelectDetails)

我得到以下错误。

AssertionError: Database queries to 'default' are not allowed in SimpleTestCase subclasses. Either subclass TestCase or TransactionTestCase to ensure proper test isolation or add 'default' to accounts.tests.tests.TestGetBanksPositive.databases to silence this failure.

数据库配置如下所示

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': '****',
    'USER': '****',
    'PASSWORD': '****',
    'HOST': '****',
    'PORT': '****',
}
}

我在哪里做错了?

标签: python-3.xdjangodatabasepostgresqlunit-testing

解决方案


推荐阅读