python - Django 单元测试 Sekizai 和 django cms

标签 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. 

You must enable the 'sekizai.context_processors.sekizai'

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

最佳答案

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

This was confusing because I was using sekizai correctly in my template.

This post pointed me in the right direction here. The problem was Django was raising an exception but I was never getting to see it – just this much less helpful message.

This Stackoverflow post explained how to enable logging of errors. I copied in the changes to settings.py, wrapping them inside the if 'test' statement.

Then when I ran

./manage.py test

I got a much more useful error message: I had forgotten to set up a table that my template was assuming would exist. Easily fixed!

Hope that helps someone else.

我需要添加一些固定装置才能让它工作。

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

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

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. Resolving View Names

Your apps need testing, but in your live site they aren’t in urls.py as they are attached to a CMS page. So if you want to be able to use reverse() in your tests, or test templates that use the url template tag, you need to hook up your app to a special test version of urls.py and tell your tests to use that.

So you could create myapp/tests/test_urls.py with the following code:

from django.contrib import admin from django.conf.urls import url, patterns, include

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

And then in your tests you can plug this in with the override_settings() decorator:

from django.test.utils import override_settings from cms.test_utils.testcases import CMSTestCase

class 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

If you want to the test url conf throughout your test class, then you can use apply the decorator to the whole class:

from django.test.utils import override_settings from cms.test_utils.testcases import CMSTestCase

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

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

关于python - Django 单元测试 Sekizai 和 django cms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16860117/

相关文章:

python - subprocess 会杀死子进程,但不会杀死子进程产生的进程

python - 在 Pandas 数据框中模糊匹配两个字符串的最快方法

python - 如何更改 3D 绘图的颜色和重命名标签

python - Django 查询不匹配

javascript - 如何将复选框设置为带有确认消息的按钮?

python - 在 python django 中使用 pdftk 填写 pdf 表单的最佳方法是什么

python - 在 Django 中限制暴力登录攻击

C# 如何使用 AutoFixture 简化单元测试字符串参数

swift - 我如何在 XCODE 8 和 Swift 3 中使用 UIAutomation

c# - 如何使用 IDataContext 接口(interface)运行存储过程?