The test for the following method checks that the stdout in prompt is correct.
When 'input()' gets called it waits for the user to press enter and interrupt. The test is passing by auto keypress-ing 'enter'.
This is hacky and there must be a better way of testing this method.
METHOD:
class GameDisplay:
@staticmethod
def prompt(text):
input_value = input(text)
return input_value
TEST:
from pynput.keyboard import Key, Controller
class TestGameDisplay(unittest.TestCase):
@patch('sys.stdout', new_callable=StringIO)
def test_prompt_output(self, mock_stdout):
keyboard = Controller()
keyboard.press(Key.enter)
self.gameDisplay.prompt('Choose 0: ')
keyboard.release(Key.enter)
self.assertEqual( mock_stdout.getvalue(), 'Choose 0: ')
if __name__ == '__main__':
unittest.main()
OUTPUT:
Breaks line as a result of pressing enter.
...................................
............................................
----------------------------------------------------------------------
Ran 79 tests in 0.113s
OK
TEST FIX ATTEMPT:
@patch('sys.stdout', new_callable=StringIO)
@patch('builtins.input', return_value='0')
def test_prompt_output(self, mock_stdout, input):
self.gameDisplay.prompt('Choose 0: ')
self.assertEqual( mock_stdout.getvalue(), 'Choose 0: ')
OUTPUT:
FAIL: test_prompt_output (tests.test_game_display.TestGameDisplay)
self.assertEqual( mock_stdout.getvalue(), 'Choose 0: ')
AssertionError: <MagicMock name='input.getvalue()' id='4675607744'> != 'Choose 0: '
----------------------------------------------------------------------
Ran 79 tests in 0.015s
FAILED (failures=1)