I'm trying to unit test a function that can have multiple repeated inputs from within a while statement:
def ask_yes_or_no(prompt):
while True:
answer = input(prompt)
if answer.capitalize()[0] == 'Y':
return True
elif answer.capitalize()[0] == 'N':
return False
def human_move(computer_score, human_score):
total = 0
roll_again = True
while roll_again:
if ask_yes_or_no('Roll again? '):
points = roll()
print('You rolled a ' + str(points))
if points == 1:
total = 0
roll_again = False
else:
total += points
else:
roll_again = False
So far I've used the mock
module with a with
statement to simulate the input. e.g.:
def test_human_move(self):
random.seed(900)
with unittest.mock.patch('builtins.input', return_value = 'N'):
self.assertEqual(human_move(0,0), 0)
However this only works for a single one time input. Is there a way to simulate a repeated input> For instance if the user input ('Y', 'Y', 'Y', 'N')?
Sorry if that's not a very clear explanation.
Thanks.