我目前正在測試我的Python代碼,並有一個關於raw_input的問題。這是我的函數:Python測試原始輸入,原始輸入if語句
def answer():
ans = raw_input('enter yes or no')
if ans == 'yes':
print 'you entered yes'
return 'yes'
if ans == 'no':
some_value = raw_input('enter some value: ')
print 'you entered no'
return some_value
我測試的第一if語句是這樣的:
with mock.patch('__builtin__.raw_input', return_value= 'yes'):
assert answer() == 'yes'
但我怎麼查不聲明?我該如何在模擬中進行模擬?
我沒有意識到'side_effect'可以帶上這樣的發電機。發電機總是最簡單的方式來捆綁簡單的狀態。 +1,只是因爲我不能+10。 – abarnert