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 | ./spellchecker.py
In the name of test automation, is there any way that pyunit can fake input to raw_input()?
The short answer is to monkey patch
raw_input().There are some good examples in the answer to How to display the redirected stdin in Python?
Here is a simple, trivial example using a
lambdathat throws away the prompt and returns what we want.System Under Test
Test case: