我有2个测试类,如下所示:
class AdminSiteTest(UserMixin, TestCase):
def setUp(self):
super().setUp()
self.user = self.create_superuser()
self.login_user()
def test_default_admin(self):
with self.settings(ROOT_URLCONF='tests.urls_admin'):
print('AdminSiteTest')
response = self.client.get('/admin/')
self.assertEqual(response.status_code, 200)
class OTPAdminSiteTest(UserMixin, TestCase):
def setUp(self):
super().setUp()
self.user = self.create_superuser()
self.login_user()
def test_otp_admin_without_otp(self):
"""
if user has admin permissions (is_staff and is_active)
but doesnt have OTP setup, redirect the user to OTP setup page
"""
with self.settings(ROOT_URLCONF='tests.urls_otp_admin'):
response2 = self.client.get('/otp_admin/', follow=True)
redirect_to = reverse('two_factor:setup')
self.assertRedirects(response2, redirect_to)
以下成功运行:
make test TARGET=tests.test_admin.OTPAdminSiteTest
make test TARGET=tests.test_admin.AdminSiteTest
以下给出了错误:
make test TARGET=tests.test_admin
详细错误:
DJANGO_SETTINGS_MODULE=tests.settings PYTHONPATH=. \
django-admin.py test tests.test_admin.*
System check identified some issues:
WARNINGS:
?: (admin.W411) 'django.template.context_processors.request' must be enabled in DjangoTemplates (TEMPLATES) in order to use the admin navigation sidebar.
System check identified 1 issue (0 silenced).
E
======================================================================
ERROR: * (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module 'tests.test_admin' has no attribute '*'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
Makefile:17: recipe for target 'test' failed
make: *** [test] Error 1
(venv) aseem@Aseem-asus:~/Code/django-two-factor-auth-fork$ make test TARGET=tests.test_admin
DJANGO_SETTINGS_MODULE=tests.settings PYTHONPATH=. \
django-admin.py test tests.test_admin
Creating test database for alias 'default'...
System check identified some issues:
WARNINGS:
?: (admin.W411) 'django.template.context_processors.request' must be enabled in DjangoTemplates (TEMPLATES) in order to use the admin navigation sidebar.
System check identified 1 issue (0 silenced).
AdminSiteTest
.F
======================================================================
FAIL: test_otp_admin_without_otp (tests.test_admin.OTPAdminSiteTest)
if user has admin permissions (is_staff and is_active)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aseem/Code/django-two-factor-auth-fork/tests/test_admin.py", line 68, in test_otp_admin_without_otp
self.assertRedirects(response2, redirect_to)
File "/home/aseem/Code/django-two-factor-auth-fork/venv/lib/python3.8/site-packages/django/test/testcases.py", line 397, in assertRedirects
self.assertURLEqual(
File "/home/aseem/Code/django-two-factor-auth-fork/venv/lib/python3.8/site-packages/django/test/testcases.py", line 417, in assertURLEqual
self.assertEqual(
AssertionError: '/admin/' != '/account/two_factor/setup/'
- /admin/
+ /account/two_factor/setup/
: Response redirected to '/admin/', expected '/account/two_factor/setup/'Expected '/admin/' to equal '/account/two_factor/setup/'.
----------------------------------------------------------------------
Ran 2 tests in 0.597s
FAILED (failures=1)
Destroying test database for alias 'default'...
Makefile:17: recipe for target 'test' failed
make: *** [test] Error 1