微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决运行整个套装时,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 举报,一经查实,本站将立刻删除。