【发布时间】:2018-08-02 18:24:55
【问题描述】:
如何使用 python unittest.mock 模拟 python 方法,该方法将返回作为参数传递的相同值,
我试过了,
from unittest.mock import MagicMock
def dummy_function(value):
"Will return same value as value passed to the function"
return value
# To moke gettext function used in template
# Then I pass this mock method to Jinja2 template to moke gettext string
_ = MagicMock(return_value=dummy_function)
当我打印 jinja 模板时,它会显示如下所示的测试,
<div class="order_details">\n
<legend class="badge"><function dummy_function at 0x10887f730></legend>\n
</div>\n
原来的 Jinja2 模板有
<div class="order_details">
<legend class="badge">_('Details')</legend>
</div>
【问题讨论】:
标签: python unit-testing mocking python-mock