早上,我正在使用来自django.test.TestCase的测试继承,并且我在使用setupclass方法时遇到问题,因为访问db并引发此错误“失败:不允许数据库访问,请使用”django_db“标记启用“,但我使用的是django_db标记。这是代码:
from django.test import TestCase
@pytest.mark.django_db
class ViewAllBeneficiariesTest(TestCase):
''' Tests for Beneficiary '''
@classmethod
def setUpClass(cls):
cls.u = model_mommy_create_user_permission_all()
def test_clients_view_all_beneficiaries_get_ok(self):
#Arrange:
self.client.login(username=self.u.username, password="admin")
#Act:
res = self.client.get('/clients/beneficiaries/all/')
#Assert:
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed(res, 'clients/search_beneficiaries.html')
def test_clients_view_all_beneficiaries_404(self):
#Arrange:
self.client.login(username=self.u.username, password="admin")
#Act:
res = self.client.get('/clients/beneficiaries/all/')
#Assert:
self.assertEqual(res.status_code, 404)
def test_clients_view_all_beneficiaries_fail(self):
#Arrange:
#Act:
res = self.client.get('/clients/beneficiaries/all/')
#Assert:
self.assertEqual(res.status_code, 302)
self.assertRedirects(res, '/accounts/login/')
答案 0 :(得分:1)
这已在pytest-django
的主分支中修复。
您可以阅读here
通过发出以下命令 -
,我可以使用pip
安装它
pip install https://github.com/pytest-dev/pytest-django/zipball/master