私が取り組んでいる Django-CMS 実装のテスト範囲を取得しようとしていますが、プラグイン/拡張機能を単体テストする方法がわかりません。誰かが以前にこれを行ったことがありますか?もしそうなら、どのように? いくつかの例は素晴らしいでしょう。
2 に答える
で示されて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
あなたの質問を正しく理解していれば、プラグインの単体テストの例は、django-cmsのインストールを保持しているフォルダーにあるモジュールcms / tests/plugins.pyにあります。
基本的に、CMSTestCaseをサブクラス化し、django.test.clientのClientクラスを使用して、CMSにリクエストを送信し、結果の応答を確認します。
クライアントの使用方法に関する情報は、http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.clientにあります。