运行整个套装时,Django测试用例失败

问题描述

我有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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...