sys.stdin sys.stdout . Python 3, StringIO, , Python 2.7 3.3+, Python 2 3 io ( , ).
, unittest.TestCase, ( ), sys.stdin/sys.stdout . :
import sys
import io
import unittest
stdin, , ( ) stdin, str:
def stub_stdin(testcase_inst, inputs):
stdin = sys.stdin
def cleanup():
sys.stdin = stdin
testcase_inst.addCleanup(cleanup)
sys.stdin = StringIO(inputs)
stdout stderr:
def stub_stdouts(testcase_inst):
stderr = sys.stderr
stdout = sys.stdout
def cleanup():
sys.stderr = stderr
sys.stdout = stdout
testcase_inst.addCleanup(cleanup)
sys.stderr = StringIO()
sys.stdout = StringIO()
, testcase addCleanup, cleanup reset , , . , , , sys.stdout io.StringIO, , , .
. , :
class ExampleTestCase(unittest.TestCase):
def test_example(self):
stub_stdin(self, '42')
stub_stdouts(self)
example()
self.assertEqual(sys.stdout.getvalue(), '42\n')
, Python 2, , StringIO StringIO, Python 3 . , , io , , , / Unicode , (, print Python 2 , ). Python 2 3:
class StringIO(io.StringIO):
"""
A "safely" wrapped version
"""
def __init__(self, value=''):
value = value.encode('utf8', 'backslashreplace').decode('utf8')
io.StringIO.__init__(self, value)
def write(self, msg):
io.StringIO.write(self, msg.encode(
'utf8', 'backslashreplace').decode('utf8'))
, unittest, Python 2, 3 ( print Python 3) stdio.
: stub_ setUp TestCase, .
, , mocks, stdin/stdout, , , .
, , , . - :
class Test(unittest.TestCase):
def helper(self, data, answer, runner):
stub_stdin(self, data)
stub_stdouts(self)
runner()
self.assertEqual(sys.stdout.getvalue(), answer)
self.doCleanups()
def test_various_inputs(self):
data_and_answers = [
('hello', 'HELLOhello'),
('goodbye', 'GOODBYEgoodbye'),
]
runScript = upperlower
for data, answer in data_and_answers:
self.helper(data, answer, runScript)
, doCleanups, , , data_and_answers , , , , - , , stdio, , . , :
def upperlower():
raw = raw_input()
print (raw.upper() + raw),
, , , , : , TestCase assertEqual , . , , , , , /, , , , for ( , - , , , ). helper - , test, , . , - /, , .
:
StringIO, StringIO, , StringIO import StringIO ( , ), , .
, , , import io, io.StringIO, class StringIO(io.StringIO). , Python 2, Python 3, , , Python 2 (, ) 5 . , , , . , , from StringIO import StringIO , StringIO StringIO. from cStringIO import StringIO , C StringIO. , , (, , Python 3).
, script. , , ( , , "" , , Python , , , ( , ), ). , , , , , - , , , , ?:) , , .
, , , /, , SO, , , , . :
, , , unittest.mock, Python 3.3+, / backport pypi, , , , , ( ) . , unittest.mock.patch StringIO patching sys.stdout.