我对Django CMS测试有一个非常奇怪的问题。当我跑:./manage.py test --settings=my_project.test_settings
时,我收到了错误:
错误:test_guest_list_view (apps.news.tests.test_views.NewsListViewTest)测试客人是否看不到 禁用的条目 -------------------------------------------------- -------------------- Traceback(最近一次调用最后一次):文件 “/home/robert/work/projects/my_project/apps/news/tests/test_views.py” 第52行,在test_guest_list_view中 response = self.client.get(self._get_list_url())文件“/home/robert/work/projects/my_project/apps/news/tests/test_views.py”, 第17行,在_get_list_url中 return reverse(“news:list”)文件“/home/robert/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/core/urlresolvers.py”, 第532行,相反 key)NoReverseMatch:u'news'不是注册名称空间
但是,当我仅为该应用程序运行测试时,一切正常 - 所有测试都通过。
到目前为止,这是我非常简单的测试类:
# -*- 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)
我在该项目的不同应用程序中具有相同的测试模式,但它不会抛出该错误。
答案 0 :(得分:2)
我找到了解决方案。
正如Django CMS docs中所建议的那样,我没有使用@override_settings(ROOT_URLCONF='myapp.tests.urls')
作为我的TestCases,而是使用了Django方式here。
因此,对于每个TestCase,我都这样做,例如:
class NewsListViewTest(CMSTestCase):
urls = 'apps.news.tests.urls'