I\'m trying to test a function that takes input from stdin
, which I\'m currently testing with something like this:
cat /usr/share/dict/words | .
Replace sys.stdin
with an instance of StringIO
, and load the StringIO
instance with the data you want returned via sys.stdin
. Also, sys.__stdin__
contains the original sys.stdin
object, so restoring sys.stdin
after your test is as simple as sys.stdin = sys.__stdin__
.
Fudge is a great python mock module, with convenient decorators for doing patching like this for you, with automatic cleanup. You should check it out.