File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -362,17 +362,29 @@ The :mod:`test.support` module defines the following functions:
362362 New optional arguments *filters * and *quiet *.
363363
364364
365- .. function :: captured_stdout()
365+ .. function :: captured_stdin()
366+ captured_stdout()
367+ captured_stderr()
366368
367- A context manager that runs the :keyword: `with ` statement body using a
368- :class: `io.StringIO ` object as sys.stdout. That object can be retrieved
369- using the ``as `` clause of the :keyword: `with ` statement.
369+ A context managers that temporarily replaces the named stream with
370+ :class: `io.StringIO ` object.
370371
371- Example use::
372+ Example use with output streams ::
372373
373- with captured_stdout() as s :
374+ with captured_stdout() as stdout, captured_stderr() as stderr :
374375 print("hello")
375- assert s.getvalue() == "hello\n"
376+ print("error", file=sys.stderr)
377+ assert stdout.getvalue() == "hello\n"
378+ assert stderr.getvalue() == "error\n"
379+
380+ Example use with input stream::
381+
382+ with captured_stdin() as stdin:
383+ stdin.write('hello\n')
384+ stdin.seek(0)
385+ # call test code that consumes from sys.stdin
386+ captured = input()
387+ self.assertEqual(captured, "hello")
376388
377389
378390.. function :: temp_cwd(name='tempcwd', quiet=False, path=None)
Original file line number Diff line number Diff line change @@ -1184,16 +1184,31 @@ def captured_output(stream_name):
11841184def captured_stdout ():
11851185 """Capture the output of sys.stdout:
11861186
1187- with captured_stdout() as s :
1187+ with captured_stdout() as stdout :
11881188 print("hello")
1189- self.assertEqual(s .getvalue(), "hello")
1189+ self.assertEqual(stdout .getvalue(), "hello\n ")
11901190 """
11911191 return captured_output ("stdout" )
11921192
11931193def captured_stderr ():
1194+ """Capture the output of sys.stderr:
1195+
1196+ with captured_stderr() as stderr:
1197+ print("hello", file=sys.stderr)
1198+ self.assertEqual(stderr.getvalue(), "hello\n ")
1199+ """
11941200 return captured_output ("stderr" )
11951201
11961202def captured_stdin ():
1203+ """Capture the input to sys.stdin:
1204+
1205+ with captured_stdin() as stdin:
1206+ stdin.write('hello\n ')
1207+ stdin.seek(0)
1208+ # call test code that consumes from sys.stdin
1209+ captured = input()
1210+ self.assertEqual(captured, "hello")
1211+ """
11971212 return captured_output ("stdin" )
11981213
11991214
Original file line number Diff line number Diff line change @@ -130,19 +130,22 @@ def test_DirsOnSysPath(self):
130130 self .assertNotIn ("bar" , sys .path )
131131
132132 def test_captured_stdout (self ):
133- with support .captured_stdout () as s :
133+ with support .captured_stdout () as stdout :
134134 print ("hello" )
135- self .assertEqual (s .getvalue (), "hello\n " )
135+ self .assertEqual (stdout .getvalue (), "hello\n " )
136136
137137 def test_captured_stderr (self ):
138- with support .captured_stderr () as s :
138+ with support .captured_stderr () as stderr :
139139 print ("hello" , file = sys .stderr )
140- self .assertEqual (s .getvalue (), "hello\n " )
140+ self .assertEqual (stderr .getvalue (), "hello\n " )
141141
142142 def test_captured_stdin (self ):
143- with support .captured_stdin () as s :
144- print ("hello" , file = sys .stdin )
145- self .assertEqual (s .getvalue (), "hello\n " )
143+ with support .captured_stdin () as stdin :
144+ stdin .write ('hello\n ' )
145+ stdin .seek (0 )
146+ # call test code that consumes from sys.stdin
147+ captured = input ()
148+ self .assertEqual (captured , "hello" )
146149
147150 def test_gc_collect (self ):
148151 support .gc_collect ()
Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ Luigi Ballabio
6565Jeff Balogh
6666Manuel Balsera
6767Matt Bandy
68+ Dmi Baranov
6869Michael J. Barber
6970Daniel Barclay
7071Nicolas Bareil
You can’t perform that action at this time.
0 commit comments