Django单元测试Sekizai和django cms

时间:2013-05-31 14:51:23

标签: python django unit-testing django-cms django-sekizai

如何在django中编写测试? 我阅读了文档:https://docs.djangoproject.com/en/dev/topics/testing/overview/

但是当我做一个简单的测试时,我们使用django-cms和sekizai:

from django.test import TestCase
from django.test.client import Client

class AccessTest(TestCase):
    def setUp(self):
        # Every test needs a client.
        self.client = Client()

    def test_details(self):
        # Issue a GET request.
        response = self.client.get('/')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

我收到此错误:

Traceback (most recent call last):   File "/home/maazza/PycharmProjects/django_my_app/search_engine/tests.py", line 18, in test_details
    response = self.client.get('/')   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/test/client.py", line 439, in get
    response = super(Client, self).get(path, data=data, **extra)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/test/client.py", line 244, in get
    return self.request(**r)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/test/client.py", line 381, in request
    response = self.handler(environ)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/test/client.py", line 84, in __call__
    response = self.get_response(request)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
    return callback(request, **param_dict)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/views/defaults.py", line 33, in server_error
    return http.HttpResponseServerError(t.render(Context({})))   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/test/utils.py", line 62, in instrumented_test_render
    return self.nodelist.render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
    return compiled_parent._render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/test/utils.py", line 62, in instrumented_test_render
    return self.nodelist.render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
    return compiled_parent._render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/test/utils.py", line 62, in instrumented_test_render
    return self.nodelist.render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/classytags/core.py", line 106, in render
    return self.render_tag(context, **kwargs)   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/sekizai/templatetags/sekizai_tags.py", line 74, in render_tag
    if not validate_context(context):   File "/home/maazza/.virtualenvs/django_my_app/local/lib/python2.7/site-packages/sekizai/templatetags/sekizai_tags.py", line 28, in validate_context
    "You must enable the 'sekizai.context_processors.sekizai' template " TemplateSyntaxError: You must enable the 'sekizai.context_processors.sekizai' template context processor or use 'sekizai.context.SekizaiContext' to render your templates. 
  

您必须启用' sekizai.context_processors.sekizai'

关于这一部分,我sekizai.context_processors.sekizai中有TEMPLATE_CONTEXT_PROCESSORS

2 个答案:

答案 0 :(得分:4)

http://racingtadpole.com/blog/testing-django-cms-sites/

  

这令人困惑,因为我正在使用sekizai   模板。

     

这篇文章指出了我正确的方向。问题是   Django提出异常,但我从来没有看到它 -   只是这个不那么有用的信息。

     

此Stackoverflow帖子解释了如何启用错误记录。一世   复制到settings.py的更改,将它们包装在if中   '测试'声明。

     

然后我跑了

     

./ manage.py test

     

我收到了一条更有用的错误信息:我忘了设置一个   我的模板假设的表将存在。轻松修复!

     

希望能帮助别人。

我需要添加一些灯具才能正常工作。

编辑: 经过大量研究后我发现,尽管有上述博客我还有不同的问题。

感谢这个答案和这个文档,我知道我必须这样做:

from django.test import TestCase
from django.test.client import Client
from cms.test_utils.testcases import CMSTestCase
from django.test.utils import override_settings
from cms.models import Page
from django.contrib import admin
from django.conf.urls import url, patterns, include
from django.conf import settings

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


class AccessTest(CMSTestCase):

    def setUp(self):
        # Every test needs a client.
        p = Page.objects.create(site_id=settings.SITE_ID, template='home_page.html')
        p.publish()
        self.client = Client()

    @override_settings(ROOT_URLCONF='search_engine.tests')
    def test_details(self):
        # Issue a GET request.
        response = self.client.get('/')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

How to unit test Django-CMS extensions?http://docs.django-cms.org/en/latest/extending_cms/testing.html< =不知何故,这只能在'最新'

中阅读
  

8.1.1。解析视图名称

     

您的应用需要测试,但在您的实际网站中,他们不在urls.py中   因为它们附加到CMS页面。所以,如果你想能够使用   测试中的reverse()或使用url模板的测试模板   标记,您需要将您的应用程序连接到urls.py的特殊测试版本   并告诉你的测试使用它。

     

因此,您可以使用以下代码创建myapp / tests / test_urls.py:

     

来自django.contrib的导入管理员来自django.conf.urls的导入网址,   模式,包括

     

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

     

然后在你的测试中你可以插入它   override_settings()装饰器:

     

来自django.test.utils的import override_settings来自   cms.test_utils.testcases导入CMSTestCase

     

类MyappTests(CMSTestCase):

@override_settings(ROOT_URLCONF='myapp.tests.test_urls')
def test_myapp_page(self):
    test_url = reverse('myapp_view_name')
    # rest of test as normal
     

如果你想在整个测试课程中测试网址,那么你   可以使用将装饰器应用于整个班级:

     

来自django.test.utils的import override_settings来自   cms.test_utils.testcases导入CMSTestCase

     

@override_settings(ROOT_URLCONF ='myapp.tests.test_urls')课程   MyappTests(CMSTestCase):

def test_myapp_page(self):
    test_url = reverse('myapp_view_name')
    # rest of test as normal

答案 1 :(得分:3)

为了解决sekizai错误,这就是我所做的:

from django.test import TestCase                                                
from sekizai.context import SekizaiContext                                      

from cms.api import add_plugin                                                  
from cms.models import Placeholder  
# Put class def here

def test_plugin(self):                                               
    placeholder = Placeholder.objects.create(slot='test')                   
    model_inst = add_plugin(placeholder,                                    
        cms_plugins.MyPlugin, 'en')                     
    context = SekizaiContext()                                              

    html = model_inst.render_plugin(context=context)                                                                                    
    self.assertTrue(len(html) > 0) 

这是一个确保在渲染插件时使用seikizai上下文处理器的情况。