1

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

  • 项目是 Django 1.11
  • 蟒蛇 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')),
]

在用户的标准 Cookiecutter 测试上运行 pytest,不添加 i18n_patterns,成功完成。像这样更新网址后:

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

然后 url 测试失败。输出如下所示:

======================================================================
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/ 而不是 /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'
        )
4

0 回答 0