Nov-06-2018, 04:41 AM
I am beginner at tests. I am trying to do a unit test for a roll die program.
I have tried many options and failed.
Here is the code:
What am i doing wrong?
Here is the test part.
I have tried many options and failed.
Here is the code:
from random import randint def run(): try: num = int(input('Enter an number to roll: ')) print('result: ', randint(1, 6)) while num != 0: num = int(input('Enter an number to roll your die, 0 to quit: ')) print('result: ', randint(1, 6)) else: return 'Game terminated!' except ValueError: return 'Only integers are allowed' #run()The program just freezes when I run the test program.
What am i doing wrong?
Here is the test part.
import pytest import rolldie from unittest import mock def test_die(): with mock.patch('builtins.input', input_value=range(1, 10)): assert rolldie.run() == range(1, 7) with mock.patch('builtins.iput', input_value=0): assert rolldie.run() == "Game terminated!" with mock.patch('builtins.iput', input_value='a'): assert rolldie.run() == "Only integers are allowed" if __name__ == '__main__': pytest.main()