-2

I've come across a niche situation while writing unit tests in python. I want to assert that the user was not prompted for input. There are plenty of examples showing how to simulate input using unittest.mock, or how to tell if methods of a user-defined object were called. However, input is a python builtin method.

What would be the best way of doing this?

RedPanda
  • 522
  • 6
  • 15

1 Answers1

0

By brute force testing, it looks like the following works:

def foo():
    return 7

with patch('builtins.input', return_value='y') as mocked:
    foo()

mocked.assert_not_called()

whereas the following will fail the test:

def foo():
    input('bar')
    return 7

with patch('builtins.input', return_value='y') as mocked:
    foo()

mocked.assert_not_called()
RedPanda
  • 522
  • 6
  • 15