2
This plugin captures stdout during test execution. If the test fails
3
or raises an error, the captured output will be appended to the error
4
or failure output. It is enabled by default but can be disabled with
5
the options ``-s`` or ``--nocapture``.
9
Don't capture stdout (any stdout output will be printed immediately)
15
from nose.plugins.base import Plugin
16
from nose.util import ln
17
from StringIO import StringIO
20
log = logging.getLogger(__name__)
22
class Capture(Plugin):
24
Output capture plugin. Enabled by default. Disable with ``-s`` or
25
``--nocapture``. This plugin captures stdout during test execution,
26
appending any output captured to the error or failure output,
27
should the test fail or raise an error.
30
env_opt = 'NOSE_NOCAPTURE'
38
def options(self, parser, env):
39
"""Register commandline options
42
"-s", "--nocapture", action="store_false",
43
default=not env.get(self.env_opt), dest="capture",
44
help="Don't capture stdout (any stdout output "
45
"will be printed immediately) [NOSE_NOCAPTURE]")
47
def configure(self, options, conf):
48
"""Configure plugin. Plugin is enabled by default.
51
if not options.capture:
54
def afterTest(self, test):
55
"""Clear capture buffer.
61
"""Replace sys.stdout with capture buffer.
63
self.start() # get an early handle on sys.stdout
65
def beforeTest(self, test):
66
"""Flush capture buffer.
70
def formatError(self, test, err):
71
"""Add captured output to error report.
73
test.capturedOutput = output = self.buffer
76
# Don't return None as that will prevent other
77
# formatters from formatting and remove earlier formatters
78
# formats, instead return the err we got
81
return (ec, self.addCaptureToErr(ev, output), tb)
83
def formatFailure(self, test, err):
84
"""Add captured output to failure report.
86
return self.formatError(test, err)
88
def addCaptureToErr(self, ev, output):
89
return '\n'.join([str(ev) , ln('>> begin captured stdout <<'),
90
output, ln('>> end captured stdout <<')])
93
self.stdout.append(sys.stdout)
94
self._buf = StringIO()
95
sys.stdout = self._buf
99
sys.stdout = self.stdout.pop()
101
def finalize(self, result):
107
def _get_buffer(self):
108
if self._buf is not None:
109
return self._buf.getvalue()
111
buffer = property(_get_buffer, None, None,
112
"""Captured stdout output.""")