enable capturing during collect

added a few xfailed tests for fixture reporting

--HG--
branch : 1.0.x
This commit is contained in:
holger krekel 2009-07-25 18:45:04 +02:00
parent 04e9197fd6
commit 9aa781907e
3 changed files with 28 additions and 42 deletions

View File

@ -10,5 +10,5 @@ Generator = py.test.collect.Generator
Function = py.test.collect.Function Function = py.test.collect.Function
Instance = py.test.collect.Instance Instance = py.test.collect.Instance
pytest_plugins = "default iocapture runner terminal keyword xfail tmpdir execnetcleanup monkeypatch recwarn pdb unittest".split() pytest_plugins = "default runner iocapture terminal keyword xfail tmpdir execnetcleanup monkeypatch recwarn pdb unittest".split()

View File

@ -34,10 +34,6 @@ def pytest_addoption(parser):
action="store_true", dest="nocapture", default=False, action="store_true", dest="nocapture", default=False,
help="disable catching of stdout/stderr during test run.") help="disable catching of stdout/stderr during test run.")
def pytest_configure(config):
if not config.option.nocapture:
config.pluginmanager.register(CapturePerTest())
def determine_capturing(config, path=None): def determine_capturing(config, path=None):
iocapture = config.getvalue("iocapture", path=path) iocapture = config.getvalue("iocapture", path=path)
if iocapture == "fd": if iocapture == "fd":
@ -50,6 +46,28 @@ def determine_capturing(config, path=None):
# how to raise errors here? # how to raise errors here?
raise config.Error("unknown io capturing: " + iocapture) raise config.Error("unknown io capturing: " + iocapture)
def pytest_make_collect_report(__call__, collector):
cap = determine_capturing(collector.config, collector.fspath)
try:
rep = __call__.execute(firstresult=True)
finally:
outerr = cap.reset()
addouterr(rep, outerr)
return rep
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())
def pytest_configure(config):
if not config.option.nocapture:
config.pluginmanager.register(CapturePerTest())
class CapturePerTest: class CapturePerTest:
def __init__(self): def __init__(self):
self.item2capture = {} self.item2capture = {}
@ -78,12 +96,8 @@ class CapturePerTest:
outerr = capture.reset() outerr = capture.reset()
# XXX shift reporting elsewhere # XXX shift reporting elsewhere
rep = __call__.execute(firstresult=True) rep = __call__.execute(firstresult=True)
if hasattr(rep, 'longrepr'): addouterr(rep, outerr)
repr = rep.longrepr
if hasattr(repr, 'addsection'):
for secname, content in zip(["out", "err"], outerr):
if content:
repr.addsection("Captured std%s" % secname, content.rstrip())
return rep return rep
def pytest_funcarg__capsys(request): def pytest_funcarg__capsys(request):
@ -91,7 +105,7 @@ def pytest_funcarg__capsys(request):
them available successively via a ``capsys.reset()`` method them available successively via a ``capsys.reset()`` method
which returns a ``(out, err)`` tuple of captured strings. which returns a ``(out, err)`` tuple of captured strings.
""" """
capture = Capture(py.io.StdCapture) capture = CaptureFuncarg(py.io.StdCapture)
request.addfinalizer(capture.finalize) request.addfinalizer(capture.finalize)
return capture return capture
@ -100,7 +114,7 @@ def pytest_funcarg__capfd(request):
them available successively via a ``capsys.reset()`` method them available successively via a ``capsys.reset()`` method
which returns a ``(out, err)`` tuple of captured strings. which returns a ``(out, err)`` tuple of captured strings.
""" """
capture = Capture(py.io.StdCaptureFD) capture = CaptureFuncarg(py.io.StdCaptureFD)
request.addfinalizer(capture.finalize) request.addfinalizer(capture.finalize)
return capture return capture
@ -110,7 +124,7 @@ def pytest_pyfunc_call(pyfuncitem):
if funcarg == "capsys" or funcarg == "capfd": if funcarg == "capsys" or funcarg == "capfd":
value.reset() value.reset()
class Capture: # funcarg class CaptureFuncarg:
_capture = None _capture = None
def __init__(self, captureclass): def __init__(self, captureclass):
self._captureclass = captureclass self._captureclass = captureclass
@ -126,30 +140,3 @@ class Capture: # funcarg
self._capture = self._captureclass() self._capture = self._captureclass()
return res return res
class TestCapture:
def test_std_functional(self, testdir):
reprec = testdir.inline_runsource("""
def test_hello(capsys):
print 42
out, err = capsys.reset()
assert out.startswith("42")
""")
reprec.assertoutcome(passed=1)
def test_stdfd_functional(self, testdir):
reprec = testdir.inline_runsource("""
def test_hello(capfd):
import os
os.write(1, "42")
out, err = capfd.reset()
assert out.startswith("42")
""")
reprec.assertoutcome(passed=1)
def test_funcall_yielded_no_funcargs(self, testdir):
reprec = testdir.inline_runsource("""
def test_hello():
yield lambda: None
""")
reprec.assertoutcome(passed=1)

View File

@ -31,7 +31,6 @@ def pytest_sessionfinish(session, exitstatus):
mod.raiseExceptions = False mod.raiseExceptions = False
def pytest_make_collect_report(collector): def pytest_make_collect_report(collector):
# XXX capturing is missing
result = excinfo = None result = excinfo = None
try: try:
result = collector._memocollect() result = collector._memocollect()