如何对 Django-CMS 扩展进行单元测试?

发布于 2024-08-28 05:50:04 字数 90 浏览 5 评论 0原文

我正在尝试为我正在开发的 Django-CMS 实现获取一些测试覆盖率,但我不确定如何对插件/扩展进行单元测试。以前有人这样做过吗?如果是,怎么做的?一些例子会很棒。

I am trying to get some test coverage for a Django-CMS implementation I'm working on and I'm unsure how to unit test plugins/extensions. Has anyone done this before, and if so, how? Some examples would be awesome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2024-09-04 05:50:04

cms/tests/plugins.py 所示的测试更像是集成测试而不是单元测试,这是相当重量级的,有时需要整个系统的很大一部分启动和运行(不一定是错误的) ,只是在调试时不切实际)。

DjangoCMS 是紧密集成的,所以我这里有一些“更接近金属”的技术,而不是一个完整的解决方案:

你需要一个 'Expando' 风格的假类:

class Expando(object): # Never use in production!
    def __init__(self, **kw):
        self.__dict__.update(kw)

要实例化你的插件类的实例:

from cms.plugin_pool import plugin_pool

# ..in production code: class YourPlugin(CMSPlugin)...

# This ensures that the system is aware of your plugin:
YrPluginCls = plugin_pool.plugins.get('YourPlugin', None)

# ..instantiate:
plugin = YrPluginCls()

健全性检查插件 .render 方法:

ctx = plugin.render({}, Expando(attr1='a1', attr2=123), None)

使用实际模板渲染,检查输出:

res = render_to_response(look.render_template, ctx)
# assert that attr1 exist in res if it should
# ..same for attr2

BeautifulSoup 在验证小 DOM 片段的内容时很方便。

使用管理表单字段间接检查模型属性是否正确运行:

from django.test.client import RequestFactory
from django.contrib.auth.models import AnonymousUser

# ...

request = RequestFactory().get('/')
request.user = AnonymousUser()
a_field = plugin.get_form(request).base_fields['a_field']
a_field.validate('<some valid value>')
# Check that a_field.validate('<some invalid value>') raises

Tests as shown by cms/tests/plugins.py is rather integration tests than unit tests, and that's quite heavy-weight and requires a sometimes too large part of the entire system up and running (not neccessary wrong, just impractical when debugging).

DjangoCMS is tightly integrated so what I have here are a few techniques to get 'closer to the metal' rather than a complete solution:

You need an 'Expando' -style fake class:

class Expando(object): # Never use in production!
    def __init__(self, **kw):
        self.__dict__.update(kw)

To instantiate an instance of your plugin class:

from cms.plugin_pool import plugin_pool

# ..in production code: class YourPlugin(CMSPlugin)...

# This ensures that the system is aware of your plugin:
YrPluginCls = plugin_pool.plugins.get('YourPlugin', None)

# ..instantiate:
plugin = YrPluginCls()

Sanity check the plugins .render method:

ctx = plugin.render({}, Expando(attr1='a1', attr2=123), None)

Render with actual template, check output:

res = render_to_response(look.render_template, ctx)
# assert that attr1 exist in res if it should
# ..same for attr2

BeautifulSoup is handy when validating content of small DOM fragments.

Use admin form fields to indirectly check that model attributes behave correctly:

from django.test.client import RequestFactory
from django.contrib.auth.models import AnonymousUser

# ...

request = RequestFactory().get('/')
request.user = AnonymousUser()
a_field = plugin.get_form(request).base_fields['a_field']
a_field.validate('<some valid value>')
# Check that a_field.validate('<some invalid value>') raises
痴情换悲伤 2024-09-04 05:50:04

如果我正确理解你的问题,你可以在 cms/tests/plugins.py 模块中找到插件单元测试的示例,该模块位于 django-cms 安装的文件夹中。

基本上,您可以继承 CMSTestCase 并使用 django.test.client 中的 Client 类向 CMS 发出请求并检查结果响应。

有关如何使用客户端的信息可以在 上找到http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client

If I understand your question correctly, you can find examples for unit-tests for plugins in the module cms/tests/plugins.py, located in the folder holding your installation of django-cms.

Basically you subclass CMSTestCase and use the Client class from django.test.client to make requests to your CMS and check the resulting responses.

Information on how to use the client can be found on http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文