fix issue128 - show captured output when capsys/capfd are in use

This commit is contained in:
holger krekel
2012-06-03 21:01:27 +02:00
parent 971f96468c
commit 3f17784386
5 changed files with 58 additions and 15 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
#
__version__ = '2.2.4'
__version__ = '2.2.5.dev1'
+24 -13
View File
@@ -121,20 +121,21 @@ class CaptureManager:
def activate_funcargs(self, pyfuncitem):
if not hasattr(pyfuncitem, 'funcargs'):
return
assert not hasattr(self, '_capturing_funcargs')
self._capturing_funcargs = capturing_funcargs = []
assert not hasattr(self, '_capturing_funcarg')
capargs = []
for name, capfuncarg in pyfuncitem.funcargs.items():
if name in ('capsys', 'capfd'):
capturing_funcargs.append(capfuncarg)
capfuncarg._start()
capargs.append(capfuncarg)
if capargs:
self._capturing_funcarg = capargs[0]
self._capturing_funcarg._start()
def deactivate_funcargs(self):
capturing_funcargs = getattr(self, '_capturing_funcargs', None)
if capturing_funcargs is not None:
while capturing_funcargs:
capfuncarg = capturing_funcargs.pop()
capfuncarg._finalize()
del self._capturing_funcargs
capturing_funcarg = getattr(self, '_capturing_funcarg', None)
if capturing_funcarg:
outerr = capturing_funcarg._finalize()
del self._capturing_funcarg
return outerr
def pytest_make_collect_report(self, __multicall__, collector):
method = self._getmethod(collector.config, collector.fspath)
@@ -169,9 +170,12 @@ class CaptureManager:
@pytest.mark.tryfirst
def pytest_runtest_makereport(self, __multicall__, item, call):
self.deactivate_funcargs()
funcarg_outerr = self.deactivate_funcargs()
rep = __multicall__.execute()
outerr = self.suspendcapture(item)
if funcarg_outerr is not None:
outerr = (outerr[0] + funcarg_outerr[0],
outerr[1] + funcarg_outerr[1])
if not rep.passed:
addouterr(rep, outerr)
if not rep.passed or rep.when == "teardown":
@@ -179,11 +183,15 @@ class CaptureManager:
item.outerr = outerr
return rep
error_capsysfderror = "cannot use capsys and capfd at the same time"
def pytest_funcarg__capsys(request):
"""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.
"""
if "capfd" in request._funcargs:
raise request.LookupError(error_capsysfderror)
return CaptureFuncarg(py.io.StdCapture)
def pytest_funcarg__capfd(request):
@@ -191,8 +199,10 @@ def pytest_funcarg__capfd(request):
captured output available via ``capsys.readouterr()`` method calls
which return a ``(out, err)`` tuple.
"""
if "capsys" in request._funcargs:
raise request.LookupError(error_capsysfderror)
if not hasattr(os, 'dup'):
py.test.skip("capfd funcarg needs os.dup")
pytest.skip("capfd funcarg needs os.dup")
return CaptureFuncarg(py.io.StdCaptureFD)
class CaptureFuncarg:
@@ -204,8 +214,9 @@ class CaptureFuncarg:
def _finalize(self):
if hasattr(self, 'capture'):
self.capture.reset()
outerr = self.capture.reset()
del self.capture
return outerr
def readouterr(self):
return self.capture.readouterr()