I want to test some (python 3) code that directly uses the print
and input
functions. As I understand it, the easiest way to do this is by dependency injection: modifying the code so that it takes input and output streams as arguments, using sys.stdin
and sys.stdout
by default and passing in mock objects during testing. It's obvious what to do with print
calls:
print(text)
#replaced with...
print(text, file=output_stream)
However, input
doesn't have arguments for input and output streams. Does the following code correctly reproduce its behaviour?
text = input(prompt)
#replaced with...
print(prompt, file=output_stream, end='')
text = input_stream.readline()[:-1]
I had a look at the implementation of input
, and it does quite a lot of magic, calling sys.stdin.fileno
and examining sys.stdin.encoding
and sys.stdin.errors
rather than calling any of the read*
methods - I wouldn't know where to start with mocking those.
input()
only does the magic you mentioned when stdin
and stdout
are not altered, because only then it can use things like the readline library. If you replace them with something else (real-files or not) it comes down to this code:
/* Fallback if we're not interactive */
if (promptarg != NULL) {
if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0)
return NULL;
}
tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
if (tmp == NULL)
PyErr_Clear();
else
Py_DECREF(tmp);
return PyFile_GetLine(fin, -1);
Where PyFile_GetLine
calls the readline
method. Thus mocking sys.std*
will work.
It's recomended you do this with try: finally:
, a context processor or the mock
module, so that the outputs are restored even if the code you are testing fails with exceptions:
from unittest.mock import patch
from io import StringIO
with patch("sys.stdin", StringIO("FOO")), patch("sys.stdout", new_callable=StringIO) as mocked_out:
x = input()
print("Read:", x)
assert mocked_out.getvalue() == "Read: FOO\n"
If you assign a file-like object to sys.stdin
Python's input
function will use it instead of the standard input. But remember to reassign sys.stdin
back to the standard input after you're done with it. The same trick applies to sys.stdout
. You can do something like this:
original_stdin = sys.stdin
sys.stdin = open('inputfile.txt', 'r')
original_stdout = sys.stdout
sys.stdout = open('outputfile.txt', 'w')
response = input('say hi: ')
print(response)
sys.stdin = original_stdin
sys.stdout = original_stdout
These two lines
response = input('say hi: ')
print(response)
will use specified files (inputfile.txt
and outputfile.txt
) instead of the standard input and standard output.
UPDATE: If you don't want to deal with physical files take a look at io
module. It provides io.StringIO
class which allows you to perform in-memory text stream operations.
original_stdin = sys.stdin
sys.stdin = io.StringIO('input string')
original_stdout = sys.stdout
sys.stdout = io.StringIO()
response = input('say hi: ')
print(response)
output = sys.stdout.getvalue()
sys.stdin = original_stdin
sys.stdout = original_stdout
print(output)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With