Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e173d012
Commit
e173d012
authored
Jul 11, 2013
by
R David Murray
Browse files
Options
Browse Files
Download
Plain Diff
Merge #17987: properly document support.captured_xxx.
parents
a155d40e
5a33f813
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
16 deletions
+47
-16
Doc/library/test.rst
Doc/library/test.rst
+19
-7
Lib/test/support.py
Lib/test/support.py
+17
-2
Lib/test/test_support.py
Lib/test/test_support.py
+10
-7
Misc/ACKS
Misc/ACKS
+1
-0
No files found.
Doc/library/test.rst
View file @
e173d012
...
@@ -362,17 +362,29 @@ The :mod:`test.support` module defines the following functions:
...
@@ -362,17 +362,29 @@ The :mod:`test.support` module defines the following functions:
New optional arguments *filters* and *quiet*.
New optional arguments *filters* and *quiet*.
.. function:: captured_stdout()
.. function:: captured_stdin()
captured_stdout()
captured_stderr()
A context manager that runs the :keyword:`with` statement body using a
A context managers that temporarily replaces the named stream with
:class:`io.StringIO` object as sys.stdout. That object can be retrieved
:class:`io.StringIO` object.
using the ``as`` clause of the :keyword:`with` statement.
Example use::
Example use
with output streams
::
with captured_stdout() as s:
with captured_stdout() as s
tdout, captured_stderr() as stderr
:
print("hello")
print("hello")
assert s.getvalue() == "hello\n"
print("error", file=sys.stderr)
assert stdout.getvalue() == "hello\n"
assert stderr.getvalue() == "error\n"
Example use with input stream::
with captured_stdin() as stdin:
stdin.write('hello\n')
stdin.seek(0)
# call test code that consumes from sys.stdin
captured = input()
self.assertEqual(captured, "hello")
.. function:: temp_cwd(name='tempcwd', quiet=False, path=None)
.. function:: temp_cwd(name='tempcwd', quiet=False, path=None)
...
...
Lib/test/support.py
View file @
e173d012
...
@@ -1186,16 +1186,31 @@ def captured_output(stream_name):
...
@@ -1186,16 +1186,31 @@ def captured_output(stream_name):
def
captured_stdout
():
def
captured_stdout
():
"""Capture the output of sys.stdout:
"""Capture the output of sys.stdout:
with captured_stdout() as s:
with captured_stdout() as s
tdout
:
print("hello")
print("hello")
self.assertEqual(s
.getvalue(), "hello
")
self.assertEqual(s
tdout.getvalue(), "hello
\
n
")
"""
"""
return
captured_output
(
"stdout"
)
return
captured_output
(
"stdout"
)
def
captured_stderr
():
def
captured_stderr
():
"""Capture the output of sys.stderr:
with captured_stderr() as stderr:
print("hello", file=sys.stderr)
self.assertEqual(stderr.getvalue(), "hello
\
n
")
"""
return
captured_output
(
"stderr"
)
return
captured_output
(
"stderr"
)
def
captured_stdin
():
def
captured_stdin
():
"""Capture the input to sys.stdin:
with captured_stdin() as stdin:
stdin.write('hello
\
n
')
stdin.seek(0)
# call test code that consumes from sys.stdin
captured = input()
self.assertEqual(captured, "hello")
"""
return
captured_output
(
"stdin"
)
return
captured_output
(
"stdin"
)
...
...
Lib/test/test_support.py
View file @
e173d012
...
@@ -130,19 +130,22 @@ class TestSupport(unittest.TestCase):
...
@@ -130,19 +130,22 @@ class TestSupport(unittest.TestCase):
self
.
assertNotIn
(
"bar"
,
sys
.
path
)
self
.
assertNotIn
(
"bar"
,
sys
.
path
)
def
test_captured_stdout
(
self
):
def
test_captured_stdout
(
self
):
with
support
.
captured_stdout
()
as
s
:
with
support
.
captured_stdout
()
as
s
tdout
:
print
(
"hello"
)
print
(
"hello"
)
self
.
assertEqual
(
s
.
getvalue
(),
"hello
\
n
"
)
self
.
assertEqual
(
s
tdout
.
getvalue
(),
"hello
\
n
"
)
def
test_captured_stderr
(
self
):
def
test_captured_stderr
(
self
):
with
support
.
captured_stderr
()
as
s
:
with
support
.
captured_stderr
()
as
s
tderr
:
print
(
"hello"
,
file
=
sys
.
stderr
)
print
(
"hello"
,
file
=
sys
.
stderr
)
self
.
assertEqual
(
s
.
getvalue
(),
"hello
\
n
"
)
self
.
assertEqual
(
s
tderr
.
getvalue
(),
"hello
\
n
"
)
def
test_captured_stdin
(
self
):
def
test_captured_stdin
(
self
):
with
support
.
captured_stdin
()
as
s
:
with
support
.
captured_stdin
()
as
stdin
:
print
(
"hello"
,
file
=
sys
.
stdin
)
stdin
.
write
(
'hello
\
n
'
)
self
.
assertEqual
(
s
.
getvalue
(),
"hello
\
n
"
)
stdin
.
seek
(
0
)
# call test code that consumes from sys.stdin
captured
=
input
()
self
.
assertEqual
(
captured
,
"hello"
)
def
test_gc_collect
(
self
):
def
test_gc_collect
(
self
):
support
.
gc_collect
()
support
.
gc_collect
()
...
...
Misc/ACKS
View file @
e173d012
...
@@ -66,6 +66,7 @@ Luigi Ballabio
...
@@ -66,6 +66,7 @@ Luigi Ballabio
Jeff Balogh
Jeff Balogh
Manuel Balsera
Manuel Balsera
Matt Bandy
Matt Bandy
Dmi Baranov
Michael J. Barber
Michael J. Barber
Daniel Barclay
Daniel Barclay
Nicolas Bareil
Nicolas Bareil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment