Skip to content

Instantly share code, notes, and snippets.

@jaapz
Last active January 2, 2016 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaapz/8317729 to your computer and use it in GitHub Desktop.
Save jaapz/8317729 to your computer and use it in GitHub Desktop.
Monkeypatch builtins for use in py.tests
import __builtin__
def test_raw_input(monkeypatch):
""" Get user input without actually having a user type letters using monkeypatch """
def mock_raw_input(*args, **kwargs):
""" Act like someone just typed 'yolo'. """
return 'yolo';
monkeypatch.setattr(__builtin__, 'raw_input', mock_raw_input)
# retval should now contain 'yolo'
retval = raw_input()
assert retval == 'yolo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment