2

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.

4

2 に答える 2

3

セキザイエラーを解決するために、これが私がしたことです:

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 コンテキスト プロセッサが使用されていることを確認するケースでした。

于 2014-11-07T19:27:17.067 に答える