How do you use the @patch decorator to patch the built-in input() function?
For example, here\'s a function in question.py that I\'d like to test, which contains a c
Or use Mock's return_value attribute. I couldn't get it to work as a decorator, but here's how to do it with a context manager:
>>> import unittest.mock
>>> def test_input_mocking():
... with unittest.mock.patch('builtins.input', return_value='y'):
... assert input() == 'y'
...
>>> def test_input_mocking():
... with unittest.mock.patch('builtins.input', return_value='y'):
... assert input() == 'y'
... print('we got here, so the ad hoc test succeeded')
...
>>> test_input_mocking()
we got here, so the ad hoc test succeeded
>>>