将Cookiecutter-django用户的网址添加到i18n_patterns中断测试

时间:2017-10-19 21:21:21

标签: django pytest-django cookiecutter-django

我在测试和国际化方面遇到了问题。

  • 项目是Django 1.11
  • Python 3.5
  • 使用cookiecutter-django创建的项目

settings.py中的国际化代码:

TIME_ZONE = 'UTC'

LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True

LANGUAGES = [
    ('en', _('English')),
    ('fr', _('French')),
    ('es', _('Spanish')),
    ('de', _('German')),
]

在不添加i18n_patterns的情况下,在用户的标准Cookiecutter测试上运行pytest成功完成。一旦网址更新如下:

urlpatterns += i18n_patterns(
    ....
    # User management
    url(r'^accounts/', include('allauth.urls')),
    url(r'^users/', include('users.urls', namespace='users')),
    ....
)

网址测试然后失败。输出如下:

======================================================================
FAIL: test_get_redirect_url (users.tests.test_views.TestUserRedirectView)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "users/tests/test_views.py", line 33, in test_get_redirect_url
    '/users/testuser/'
AssertionError: '/en-us/users/testuser/' != '/users/testuser/'
- /en-us/users/testuser/
? ------
+ /users/testuser/

如您所见,问题是/ en-us /是url上使用的前缀。现在,我可以通过在我的断言中添加/ en-us /来解决这个问题。但是,/ en-us /前缀不正确。这是一个语言代码,而不是我的设置文件中显示的常规语言前缀。

在测试之外,并使用runserver,如果我导航到/ users // url,则以/ en / not / en-us /

开头

所以,最大的问题是为什么i18n_patterns()使用/ en /时,测试前面是/ en-us /?

修改 以下是users / tests / test_urls.py

的内容
from django.core.urlresolvers import reverse, resolve

from test_plus.test import TestCase


class TestUserURLs(TestCase):
    """Test URL patterns for users app."""

    def setUp(self):
        self.user = self.make_user()

    def test_list_reverse(self):
        """users:list should reverse to /users/."""
        self.assertEqual(reverse('users:list'), '/users/')

    def test_list_resolve(self):
        """/users/ should resolve to users:list."""
        self.assertEqual(resolve('/users/').view_name, 'users:list')

    def test_redirect_reverse(self):
        """users:redirect should reverse to /users/~redirect/."""
        self.assertEqual(reverse('users:redirect'), '/users/~redirect/')

    def test_redirect_resolve(self):
        """/users/~redirect/ should resolve to users:redirect."""
        self.assertEqual(
            resolve('/users/~redirect/').view_name,
            'users:redirect'
        )

    def test_detail_reverse(self):
        """users:detail should reverse to /users/testuser/."""
        self.assertEqual(
            reverse('users:detail', kwargs={'username': 'testuser'}),
            '/users/testuser/'
        )

    def test_detail_resolve(self):
        """/users/testuser/ should resolve to users:detail."""
        self.assertEqual(resolve('/users/testuser/').view_name, 'users:detail')

    def test_update_reverse(self):
        """users:update should reverse to /users/~update/."""
        self.assertEqual(reverse('users:update'), '/users/~update/')

    def test_update_resolve(self):
        """/users/~update/ should resolve to users:update."""
        self.assertEqual(
            resolve('/users/~update/').view_name,
            'users:update'
        )

0 个答案:

没有答案