- Django 版本:v1.10
- Python:3.5(事实证明这很重要..看看答案)
我找到这个答案是为了有一个 if-else 标签来比较 Django 模板中的 request.path
https://stackoverflow.com/a/19895344/80353
from django import template
from django.template.base import Node, NodeList, TemplateSyntaxError
register = template.Library()
class IfCurrentViewNode(Node):
child_nodelists = ('nodelist_true', 'nodelist_false')
def __init__(self, view_name, nodelist_true, nodelist_false):
self.view_name = view_name
self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
def __repr__(self):
return "<IfCurrentViewNode>"
def render(self, context):
view_name = self.view_name.resolve(context, True)
request = context['request']
if request.resolver_match.url_name == view_name:
return self.nodelist_true.render(context)
return self.nodelist_false.render(context)
def do_ifcurrentview(parser, token):
bits = token.split_contents()
if len(bits) < 2:
raise TemplateSyntaxError("'%s' takes at least one argument"
" (path to a view)" % bits[0])
view_name = parser.compile_filter(bits[1])
nodelist_true = parser.parse(('else', 'endifcurrentview'))
token = parser.next_token()
if token.contents == 'else':
nodelist_false = parser.parse(('endifcurrentview',))
parser.delete_first_token()
else:
nodelist_false = NodeList()
return IfCurrentViewNode(view_name, nodelist_true, nodelist_false)
@register.tag
def ifcurrentview(parser, token):
"""
Outputs the contents of the block if the current view match the argument.
Examples::
{% ifcurrentview 'path.to.some_view' %}
...
{% endifcurrentview %}
{% ifcurrentview 'path.to.some_view' %}
...
{% else %}
...
{% endifcurrentview %}
"""
return do_ifcurrentview(parser, token)
想知道是否有一种方法可以编写测试用例来覆盖此自定义代码?
我想保持我们的测试覆盖率
最佳答案
您应该能够使用 mock 覆盖大部分(如果不是全部)。例如,假设您要测试渲染功能:
from unittest.mock import Mock
def test_render_url_match(self):
mock_request = Mock()
matching_url_name = 'url_name'
mock_nodelist_true, mock_nodelist_false = Mock(), Mock()
mock_view_name = Mock()
mock_view_name.resolve.return_value = matching_url_name
mock_request.resolver_match.url_name = matching_url_name
mock_context = {'request': mock_request}
custom_node = IfCurrentViewNode(mock_view_name, mock_nodelist_true, mock_nodelist_false)
custom_node.render(mock_context)
# You can then test that the correct function was called:
# you can change to `assert_called_once` if using Python 3.6
mock_nodelist_true.render.assert_called_once_with(mock_context)
通过以这种方式设置模拟,我确保此 request.resolver_match.url_name == view_name
为真,并点击此行:return self.nodelist_true.render (上下文)
。然后您可以设置 url 名称,使它们不匹配并覆盖错误的情况。
然后,对于 do_ifcurrentview
函数,您也可以模拟出您需要的任何部分。也许您不想让 parser.compile_filter
返回您需要的内容。只需模拟它并更改返回值:
with mock.patch('parser.compile_filter') as mock_compile_filter:
mock_compile_filter.return_value = 'my_expected_view_name'
关于python - 如何为这个 Django 自定义标签编写测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534145/