如何为此Django自定义标签编写测试用例 [英] How to write a test case for this Django custom tag
问题描述
- Django版本:v1.10
- Python:3.5(这很重要,因为事实证明..查看答案)
我找到了这个答案,以便拥有一个if-else标签来比较Django模板中的request.path
I found this answer in order to have a if-else tag to compare the request.path in Django templates
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)
是否想知道是否有一种方法可以编写测试用例来覆盖此自定义代码?
Was wondering if there's a way to write test case to cover this custom code?
我想保持测试覆盖率
推荐答案
您应该能够使用模拟功能涵盖大部分(如果不是全部)内容.例如,假设您要测试渲染功能:
You should be able to cover most, if not all of that using mock. For example, let's say you want to test the render function:
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(上下文)
.然后,您可以设置网址名称,以使它们不匹配并覆盖错误的大小写.
By setting up the mocks that way, I've ensured that this request.resolver_match.url_name == view_name
will be true, and hit this line: return self.nodelist_true.render(context)
. You can then set up the url name so they don't match and cover the false case.
然后,对于 do_ifcurrentview
函数,您也可以模拟出所需的任何部分.也许您不想让 parser.compile_filter
返回您所需要的东西.只是模拟它并更改返回值:
Then, for the do_ifcurrentview
function, you can mock out whatever pieces you need to as well. Maybe you don't want to mess with getting parser.compile_filter
to return what you need. Just mock it and change the return value:
with mock.patch('parser.compile_filter') as mock_compile_filter:
mock_compile_filter.return_value = 'my_expected_view_name'
这篇关于如何为此Django自定义标签编写测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!