2010-11-06 18:38:53 +08:00
|
|
|
""" per-test stdout/stderr capturing mechanisms, ``capsys`` and ``capfd`` function arguments. """
|
2009-07-21 00:54:08 +08:00
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
import pytest, py
|
2009-09-05 22:54:52 +08:00
|
|
|
import os
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("general")
|
2009-07-31 20:21:02 +08:00
|
|
|
group._addoption('--capture', action="store", default=None,
|
2009-08-03 17:56:56 +08:00
|
|
|
metavar="method", type="choice", choices=['fd', 'sys', 'no'],
|
2010-01-03 19:41:29 +08:00
|
|
|
help="per-test capturing method: one of fd (default)|sys|no.")
|
2010-07-27 03:15:15 +08:00
|
|
|
group._addoption('-s', action="store_const", const="no", dest="capture",
|
2009-10-17 23:43:59 +08:00
|
|
|
help="shortcut for --capture=no.")
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2009-07-26 00:45:04 +08:00
|
|
|
def addouterr(rep, outerr):
|
|
|
|
repr = getattr(rep, 'longrepr', None)
|
|
|
|
if not hasattr(repr, 'addsection'):
|
|
|
|
return
|
|
|
|
for secname, content in zip(["out", "err"], outerr):
|
|
|
|
if content:
|
|
|
|
repr.addsection("Captured std%s" % secname, content.rstrip())
|
|
|
|
|
2010-10-26 05:09:24 +08:00
|
|
|
def pytest_unconfigure(config):
|
2011-01-18 19:51:21 +08:00
|
|
|
# registered in config.py during early conftest.py loading
|
2010-10-26 05:09:24 +08:00
|
|
|
capman = config.pluginmanager.getplugin('capturemanager')
|
|
|
|
while capman._method2capture:
|
|
|
|
name, cap = capman._method2capture.popitem()
|
2010-12-06 23:56:12 +08:00
|
|
|
# XXX logging module may wants to close it itself on process exit
|
|
|
|
# otherwise we could do finalization here and call "reset()".
|
|
|
|
cap.suspend()
|
2010-10-26 05:09:24 +08:00
|
|
|
|
2010-05-18 01:00:39 +08:00
|
|
|
class NoCapture:
|
|
|
|
def startall(self):
|
|
|
|
pass
|
|
|
|
def resume(self):
|
|
|
|
pass
|
2010-10-26 05:09:24 +08:00
|
|
|
def reset(self):
|
|
|
|
pass
|
2010-05-18 01:00:39 +08:00
|
|
|
def suspend(self):
|
|
|
|
return "", ""
|
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
class CaptureManager:
|
2009-07-26 00:09:01 +08:00
|
|
|
def __init__(self):
|
2009-07-31 20:21:02 +08:00
|
|
|
self._method2capture = {}
|
|
|
|
|
2009-08-06 20:34:19 +08:00
|
|
|
def _maketempfile(self):
|
|
|
|
f = py.std.tempfile.TemporaryFile()
|
2010-07-27 03:15:15 +08:00
|
|
|
newf = py.io.dupfile(f, encoding="UTF-8")
|
2010-10-26 05:09:24 +08:00
|
|
|
f.close()
|
2009-08-29 21:51:49 +08:00
|
|
|
return newf
|
2009-08-06 20:34:19 +08:00
|
|
|
|
|
|
|
def _makestringio(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
return py.io.TextIO()
|
2009-08-06 20:34:19 +08:00
|
|
|
|
2010-05-18 01:00:39 +08:00
|
|
|
def _getcapture(self, method):
|
2010-07-27 03:15:15 +08:00
|
|
|
if method == "fd":
|
2010-05-18 01:00:39 +08:00
|
|
|
return py.io.StdCaptureFD(now=False,
|
2009-08-06 20:34:19 +08:00
|
|
|
out=self._maketempfile(), err=self._maketempfile()
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
elif method == "sys":
|
2010-05-18 01:00:39 +08:00
|
|
|
return py.io.StdCapture(now=False,
|
2009-08-06 20:34:19 +08:00
|
|
|
out=self._makestringio(), err=self._makestringio()
|
|
|
|
)
|
2010-05-18 01:00:39 +08:00
|
|
|
elif method == "no":
|
|
|
|
return NoCapture()
|
2009-07-31 20:21:02 +08:00
|
|
|
else:
|
|
|
|
raise ValueError("unknown capturing method: %r" % method)
|
|
|
|
|
2011-01-18 19:51:21 +08:00
|
|
|
def _getmethod_preoptionparse(self, args):
|
|
|
|
if '-s' in args or "--capture=no" in args:
|
|
|
|
return "no"
|
|
|
|
elif hasattr(os, 'dup') and '--capture=sys' not in args:
|
|
|
|
return "fd"
|
|
|
|
else:
|
|
|
|
return "sys"
|
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def _getmethod(self, config, fspath):
|
|
|
|
if config.option.capture:
|
2009-09-05 22:54:52 +08:00
|
|
|
method = config.option.capture
|
|
|
|
else:
|
2010-07-27 03:15:15 +08:00
|
|
|
try:
|
2009-09-05 22:54:52 +08:00
|
|
|
method = config._conftest.rget("option_capture", path=fspath)
|
|
|
|
except KeyError:
|
|
|
|
method = "fd"
|
2010-07-27 03:15:15 +08:00
|
|
|
if method == "fd" and not hasattr(os, 'dup'): # e.g. jython
|
|
|
|
method = "sys"
|
2009-09-05 22:54:52 +08:00
|
|
|
return method
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def resumecapture_item(self, item):
|
|
|
|
method = self._getmethod(item.config, item.fspath)
|
|
|
|
if not hasattr(item, 'outerr'):
|
|
|
|
item.outerr = ('', '') # we accumulate outerr on the item
|
|
|
|
return self.resumecapture(method)
|
|
|
|
|
|
|
|
def resumecapture(self, method):
|
|
|
|
if hasattr(self, '_capturing'):
|
2010-07-27 03:15:15 +08:00
|
|
|
raise ValueError("cannot resume, already capturing with %r" %
|
2009-07-31 20:21:02 +08:00
|
|
|
(self._capturing,))
|
2010-05-18 01:00:39 +08:00
|
|
|
cap = self._method2capture.get(method)
|
2010-07-27 03:15:15 +08:00
|
|
|
self._capturing = method
|
2010-05-18 01:00:39 +08:00
|
|
|
if cap is None:
|
|
|
|
self._method2capture[method] = cap = self._getcapture(method)
|
|
|
|
cap.startall()
|
|
|
|
else:
|
|
|
|
cap.resume()
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2009-12-30 07:11:27 +08:00
|
|
|
def suspendcapture(self, item=None):
|
2009-07-31 20:21:02 +08:00
|
|
|
self.deactivate_funcargs()
|
2009-12-30 07:11:27 +08:00
|
|
|
if hasattr(self, '_capturing'):
|
|
|
|
method = self._capturing
|
2010-05-18 01:00:39 +08:00
|
|
|
cap = self._method2capture.get(method)
|
|
|
|
if cap is not None:
|
2009-12-30 07:11:27 +08:00
|
|
|
outerr = cap.suspend()
|
|
|
|
del self._capturing
|
|
|
|
if item:
|
2010-07-27 03:15:15 +08:00
|
|
|
outerr = (item.outerr[0] + outerr[0],
|
2010-05-18 01:00:39 +08:00
|
|
|
item.outerr[1] + outerr[1])
|
2010-07-27 03:15:15 +08:00
|
|
|
return outerr
|
2010-10-06 20:48:24 +08:00
|
|
|
if hasattr(item, 'outerr'):
|
|
|
|
return item.outerr
|
2009-12-30 07:11:27 +08:00
|
|
|
return "", ""
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def activate_funcargs(self, pyfuncitem):
|
|
|
|
if not hasattr(pyfuncitem, 'funcargs'):
|
|
|
|
return
|
|
|
|
assert not hasattr(self, '_capturing_funcargs')
|
2010-05-18 01:00:39 +08:00
|
|
|
self._capturing_funcargs = capturing_funcargs = []
|
|
|
|
for name, capfuncarg in pyfuncitem.funcargs.items():
|
2009-07-31 20:21:02 +08:00
|
|
|
if name in ('capsys', 'capfd'):
|
2010-05-18 01:00:39 +08:00
|
|
|
capturing_funcargs.append(capfuncarg)
|
|
|
|
capfuncarg._start()
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def deactivate_funcargs(self):
|
2010-05-18 01:00:39 +08:00
|
|
|
capturing_funcargs = getattr(self, '_capturing_funcargs', None)
|
|
|
|
if capturing_funcargs is not None:
|
|
|
|
while capturing_funcargs:
|
|
|
|
capfuncarg = capturing_funcargs.pop()
|
2009-07-31 20:21:02 +08:00
|
|
|
capfuncarg._finalize()
|
|
|
|
del self._capturing_funcargs
|
|
|
|
|
2009-08-12 01:00:41 +08:00
|
|
|
def pytest_make_collect_report(self, __multicall__, collector):
|
2009-07-31 20:21:02 +08:00
|
|
|
method = self._getmethod(collector.config, collector.fspath)
|
2010-11-06 16:58:04 +08:00
|
|
|
try:
|
|
|
|
self.resumecapture(method)
|
|
|
|
except ValueError:
|
|
|
|
return # recursive collect, XXX refactor capturing
|
|
|
|
# to allow for more lightweight recursive capturing
|
2009-07-31 20:21:02 +08:00
|
|
|
try:
|
2009-08-12 01:00:41 +08:00
|
|
|
rep = __multicall__.execute()
|
2009-07-31 20:21:02 +08:00
|
|
|
finally:
|
|
|
|
outerr = self.suspendcapture()
|
|
|
|
addouterr(rep, outerr)
|
|
|
|
return rep
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
@pytest.mark.tryfirst
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_runtest_setup(self, item):
|
2009-07-31 20:21:02 +08:00
|
|
|
self.resumecapture_item(item)
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
@pytest.mark.tryfirst
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_runtest_call(self, item):
|
2009-07-31 20:21:02 +08:00
|
|
|
self.resumecapture_item(item)
|
|
|
|
self.activate_funcargs(item)
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
@pytest.mark.tryfirst
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_runtest_teardown(self, item):
|
2009-07-31 20:21:02 +08:00
|
|
|
self.resumecapture_item(item)
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2009-08-12 01:00:41 +08:00
|
|
|
def pytest__teardown_final(self, __multicall__, session):
|
2009-07-31 20:22:02 +08:00
|
|
|
method = self._getmethod(session.config, None)
|
|
|
|
self.resumecapture(method)
|
|
|
|
try:
|
2009-08-12 01:00:41 +08:00
|
|
|
rep = __multicall__.execute()
|
2009-07-31 20:22:02 +08:00
|
|
|
finally:
|
|
|
|
outerr = self.suspendcapture()
|
|
|
|
if rep:
|
|
|
|
addouterr(rep, outerr)
|
|
|
|
return rep
|
|
|
|
|
2009-07-26 00:09:01 +08:00
|
|
|
def pytest_keyboard_interrupt(self, excinfo):
|
2009-07-31 20:21:02 +08:00
|
|
|
if hasattr(self, '_capturing'):
|
|
|
|
self.suspendcapture()
|
2009-07-26 00:09:01 +08:00
|
|
|
|
2010-11-22 06:17:59 +08:00
|
|
|
@pytest.mark.tryfirst
|
2009-08-12 01:00:41 +08:00
|
|
|
def pytest_runtest_makereport(self, __multicall__, item, call):
|
2009-07-31 20:21:02 +08:00
|
|
|
self.deactivate_funcargs()
|
2009-08-12 01:00:41 +08:00
|
|
|
rep = __multicall__.execute()
|
2009-12-30 07:11:27 +08:00
|
|
|
outerr = self.suspendcapture(item)
|
2009-07-31 20:21:02 +08:00
|
|
|
if not rep.passed:
|
|
|
|
addouterr(rep, outerr)
|
|
|
|
if not rep.passed or rep.when == "teardown":
|
|
|
|
outerr = ('', '')
|
2010-07-27 03:15:15 +08:00
|
|
|
item.outerr = outerr
|
2009-07-26 00:09:01 +08:00
|
|
|
return rep
|
|
|
|
|
2009-05-21 05:12:37 +08:00
|
|
|
def pytest_funcarg__capsys(request):
|
2011-03-04 06:40:38 +08:00
|
|
|
"""enables capturing of writes to sys.stdout/sys.stderr and makes
|
|
|
|
captured output available via ``capsys.readouterr()`` method calls
|
|
|
|
which return a ``(out, err)`` tuple.
|
2010-07-27 03:15:15 +08:00
|
|
|
"""
|
2010-10-05 23:56:37 +08:00
|
|
|
return CaptureFuncarg(py.io.StdCapture)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-21 05:12:37 +08:00
|
|
|
def pytest_funcarg__capfd(request):
|
2011-03-04 06:40:38 +08:00
|
|
|
"""enables capturing of writes to file descriptors 1 and 2 and makes
|
|
|
|
captured output available via ``capsys.readouterr()`` method calls
|
|
|
|
which return a ``(out, err)`` tuple.
|
2010-07-27 03:15:15 +08:00
|
|
|
"""
|
2010-05-18 01:00:39 +08:00
|
|
|
if not hasattr(os, 'dup'):
|
|
|
|
py.test.skip("capfd funcarg needs os.dup")
|
2010-10-05 23:56:37 +08:00
|
|
|
return CaptureFuncarg(py.io.StdCaptureFD)
|
2009-06-09 22:08:34 +08:00
|
|
|
|
2009-07-26 00:45:04 +08:00
|
|
|
class CaptureFuncarg:
|
2010-10-05 23:56:37 +08:00
|
|
|
def __init__(self, captureclass):
|
|
|
|
self.capture = captureclass(now=False)
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def _start(self):
|
2010-05-18 01:00:39 +08:00
|
|
|
self.capture.startall()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def _finalize(self):
|
|
|
|
if hasattr(self, 'capture'):
|
|
|
|
self.capture.reset()
|
2010-07-27 03:15:15 +08:00
|
|
|
del self.capture
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def readouterr(self):
|
|
|
|
return self.capture.readouterr()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def close(self):
|
2010-05-18 01:00:39 +08:00
|
|
|
self._finalize()
|