0

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.

TobyBBrown
  • 157
  • 4
  • 10
  • This will help you: https://stackoverflow.com/questions/24897145/python-mock-multiple-return-values – Tiger_Mike Nov 10 '17 at 14:20
  • @Tiger_Mike Perfect, probably should have been able to find that before posting the question, oh well! Thanks! – TobyBBrown Nov 10 '17 at 16:20
  • Possible duplicate of [Python mock multiple return values](https://stackoverflow.com/questions/24897145/python-mock-multiple-return-values) – Eugene Primako Jul 08 '18 at 14:20

0 Answers0