I'm studying Python and a few weeks ago I had created a game which the user needs to guess the number between an interval defined by the user himself. Now that I'm learning about Unittest, I decided to write a test module for the game. However, as it takes 4 inputs from the user (two of them defines the range which the random number will be generated, one is the user's guess and the last is an Y/N question for the user to decide whether or not he wants to continue.
import random
def main():
print('Welcome to the guess game!')
while True:
try:
low_param = int(input('Please enter the lower number: '))
high_param = int(input('Please enter the higher number: '))
if high_param <= low_param:
print('No, first the lower number, then the higher number!')
else:
break
except:
print('You need to enter a number!')
while True:
try:
result = random.randint(low_param, high_param)
guess = int(input(f'Please enter a number between {low_param} and {high_param}: '))
if low_param <= guess <= high_param:
if result == guess:
print('Nice, dude!')
break
else:
print ('Not yet, chap')
while True:
try_again = input('Would you like to try again? (Y/N) ')
if try_again.lower() == 'n':
break
elif try_again.lower() == 'y':
print('If you consider yourself capable...')
break
else:
pass
if try_again.lower() == 'n':
print('Ok, maybe next time, pal :v')
break
else:
print(f'Your guess must be between {low_param} and {high_param}')
except:
print('Are you sure you entered a number?')
if __name__ == '__main__':
main()
On the tests, I want to create some methods to verify the following situations:
1 - low_param or high_param aren't numbers 2 - low_param is higher than high_param 3 - guess is higher than high_param 4 - guess is lower than low_param 5 - guess is a string 6 - try_again is neither Y nor N
I managed to mock one input on the first method, however I don't know how to assert with a print statement as the situation output. For the other situations I need to mock more than one input, and there I got stuck.
How can I solve those two problems?
import unittest
from unittest.mock import patch
from randomgame import main
class TestRandom(unittest.TestCase):
@patch('randomgame.input', create = True)
def test_params_input_1(self, mock_input):
mock_input.side_effect = ['foo']
result = main()
self.assertEqual(result, 'You need to enter a number!')
@patch('randomgame.input2', create = True)
def test_params_input_2(self, mock_inputs_2):
mock_inputs_2.side_effect = [1 , 0]
result = main()
self.assertEqual(result, 'No, first the lower number, then the higher number!')
if __name__ == '__main__':
unittest.main()