cof*_*der 10 python unit-testing input mocking python-3.x
你如何使用@patch装饰器来修补内置的input()函数?
例如,这是我要测试的question.py中的一个函数,它包含对input()的调用:
def query_yes_no(question, default="yes"):
""" Adapted from http://stackoverflow.com/questions/3041986/python-command-line-yes-no-input """
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
Run Code Online (Sandbox Code Playgroud)
这是我的测试,它给出了错误"ImportError:没有名为' builtins '的模块":
import unittest
from unittest.mock import patch
import question
class TestQueryYesNo(unittest.TestCase):
@patch('__builtins__.input.return_value', 'y')
def test_query_y(self):
answer = question.query_yes_no("Blah?")
self.assertTrue(answer)
Run Code Online (Sandbox Code Playgroud)
fal*_*tru 16
__builtin__模块在Python 3中重命名为builtins.替换如下:
@patch('builtins.input', lambda *args: 'y')
Run Code Online (Sandbox Code Playgroud)
UPDATE
input有一个可选参数.更新代码以接受可选参数.
或者使用 Mock 的return_value属性。我无法让它作为装饰器工作,但这里是如何使用上下文管理器来做到这一点:
>>> import unittest.mock
>>> def test_input_mocking():
... with unittest.mock.patch('builtins.input', return_value='y'):
... assert input() == 'y'
...
>>> def test_input_mocking():
... with unittest.mock.patch('builtins.input', return_value='y'):
... assert input() == 'y'
... print('we got here, so the ad hoc test succeeded')
...
>>> test_input_mocking()
we got here, so the ad hoc test succeeded
>>>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7140 次 |
| 最近记录: |