gpt4 book ai didi

python - Django CMS 测试 - 找不到命名空间

转载 作者:行者123 更新时间:2023-12-01 05:02:44 24 4
gpt4 key购买 nike

我在 Django CMS 测试中遇到了一个非常奇怪的问题。当我运行: ./manage.py test --settings=my_project.test_settings 我收到该错误:

ERROR: test_guest_list_view (apps.news.tests.test_views.NewsListViewTest) Tests if guest can't see disabled entries ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/robert/work/projects/my_project/apps/news/tests/test_views.py", line 52, in test_guest_list_view response = self.client.get(self._get_list_url()) File "/home/robert/work/projects/my_project/apps/news/tests/test_views.py", line 17, in _get_list_url return reverse("news:list") File "/home/robert/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 532, in reverse key) NoReverseMatch: u'news' is not a registered namespace

但是当我仅对该应用程序运行测试时,一切正常 - 所有测试都通过。

这是我到目前为止非常简单的测试类:

# -*- coding: utf-8 -*-
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.test.utils import override_settings

from cms.test_utils.testcases import CMSTestCase

from apps.accounts.tests.factories import CustomUserFactory
from .factories import NewsFactory
from ..models import News


class BaseNewsTestCase(CMSTestCase):
def _get_list_url(self):
"""Returns URL to objects list"""
return reverse("news:list")

def _create_data_structure(self):
"""Created test data"""
# add objects
self.disabled = NewsFactory(is_visible=False)
self.enabled = NewsFactory()
NewsFactory()
NewsFactory()

self.user = CustomUserFactory(username='user', password='user')

# privileged_user
self.privileged_user = CustomUserFactory(username='p_user',
password='p_user')

# add permissions
content_type = ContentType.objects.get_for_model(News)
permissions_list = ('add_news', 'change_news', 'delete_news')
permissions = Permission.objects.filter(content_type=content_type,
codename__in=permissions_list)

self.privileged_user.user_permissions.add(*permissions)


@override_settings(ROOT_URLCONF='apps.news.tests.urls')
class NewsListViewTest(BaseNewsTestCase):
def test_guest_list_view(self):
"""Tests if guest can't see disabled entries"""
self._create_data_structure()

response = self.client.get(self._get_list_url())
objects = response.context['object_list']

self.assertEqual(len(objects), 3)
for obj in objects:
self.assertNotEqual(obj, self.disabled)

和测试网址:

# -*- coding: utf-8 -*-
from django.contrib import admin
from django.conf.urls import url, patterns, include

urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', include('apps.news.urls', namespace='news')),
url(r'', include('cms.urls')),
)

有什么线索会导致这个问题吗?我按照这个来测试我的 CMA apphook ( http://django-cms.readthedocs.org/en/latest/extending_cms/testing.html )

我在该项目的不同应用程序中有相同的测试模式,但它不会抛出该错误。

最佳答案

我找到了解决方案。而不是按照 Django CMS docs 中的建议对我的测试用例使用 @override_settings(ROOT_URLCONF='myapp.tests.urls')我用Django的方式找到了here .

因此,对于每个测试用例,我都会这样做,例如:

class NewsListViewTest(CMSTestCase):
urls = 'apps.news.tests.urls'

关于python - Django CMS 测试 - 找不到命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25732990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com