blob: a10a7953a17becd31c71ee793b549c4a13652655 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
from contextlib import contextmanager
import sys
from six import StringIO
@contextmanager
def captured_output(stream_name):
"""Return a context manager used by captured_stdout/stdin/stderr
that temporarily replaces the sys stream *stream_name* with a StringIO.
This function and the following ``captured_std*`` are copied
from CPython's ``test.support`` module.
"""
orig_stdout = getattr(sys, stream_name)
setattr(sys, stream_name, StringIO())
try:
yield getattr(sys, stream_name)
finally:
setattr(sys, stream_name, orig_stdout)
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')
|