以下是测试用例代码:
def test_interactive_session_set_language(monkeypatch):
"""Test user possibility to select language."""
events = [':ru-en', EOFError]
mock_input = mock.Mock(side_effect=events)
mock_set_language = mock.Mock()
monkeypatch.setattr('builtins.input', mock_input)
monkeypatch.setattr('pylingvo.interactive.pylingvo.client.set_language',
mock_set_language)
with pytest.raises(SystemExit):
pylingvo.interactive.start_session()
assert mock_set_language.call_count == 1
这是一个模块代码:
import pylingvo.client
import sys
import re
def start_session():
"""Start interactive session."""
while True:
try:
line = input('> ')
except (EOFError, KeyboardInterrupt):
sys.exit()
else:
command = dispatch(line)
COMMAND_MATCH_PATTERNS = [
('^:[a-z]{2}-[a-z]{2}$', pylingvo.client.set_language),
('^.*$', Exception),
]
COMMAND_MATCH_TABLE = [(re.compile(regex), command)
for regex, command in COMMAND_MATCH_PATTERNS]
def dispatch(command_name):
"""Convert interactive input to callable object."""
for pattern, command in COMMAND_MATCH_TABLE:
if pattern.match(command_name):
return command
此测试用例因此消息失败:
E assert 0 == 1
E + where 0 = <Mock id='139806765631632'>.call_count
我们可以看到模拟对象没有调用到期测试运行。所以我的问题 是如何正确设置模拟属性然后这个测试用例可以通过?
答案 0 :(得分:0)
看起来你正在修补错误的目标。试试这个:
monkeypatch.setattr('pylingvo.client.set_language', mock_set_language)