2009-05-21 05:12:37 +08:00
|
|
|
"""
|
|
|
|
'capsys' and 'capfd' funcargs for capturing stdout/stderror either
|
|
|
|
by intercepting sys.stdout/stderr or File Descriptors 1/2.
|
|
|
|
|
|
|
|
Calling the reset() method of the capture funcargs gives
|
|
|
|
a out/err tuple of strings representing the captured streams.
|
|
|
|
You can call reset() multiple times each time getting
|
|
|
|
the chunk of output that was captured between the invocations.
|
|
|
|
|
|
|
|
"""
|
2009-02-27 18:18:27 +08:00
|
|
|
import py
|
|
|
|
|
2009-05-21 05:12:37 +08:00
|
|
|
def pytest_funcarg__capsys(request):
|
2009-05-19 05:26:16 +08:00
|
|
|
""" capture writes to sys.stdout/sys.stderr. """
|
|
|
|
capture = Capture(py.io.StdCapture)
|
|
|
|
request.addfinalizer(capture.finalize)
|
|
|
|
return capture
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-21 05:12:37 +08:00
|
|
|
def pytest_funcarg__capfd(request):
|
2009-05-19 05:26:16 +08:00
|
|
|
""" capture writes to filedescriptors 1 and 2"""
|
|
|
|
capture = Capture(py.io.StdCaptureFD)
|
|
|
|
request.addfinalizer(capture.finalize)
|
|
|
|
return capture
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class Capture:
|
|
|
|
def __init__(self, captureclass):
|
|
|
|
self._captureclass = captureclass
|
|
|
|
self._capture = self._captureclass()
|
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
self._capture.reset()
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
res = self._capture.reset()
|
|
|
|
self._capture = self._captureclass()
|
|
|
|
return res
|
|
|
|
|
|
|
|
def test_generic(plugintester):
|
2009-05-19 05:26:16 +08:00
|
|
|
plugintester.hookcheck()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TestCapture:
|
|
|
|
def test_std_functional(self, testdir):
|
2009-05-20 01:25:21 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2009-05-21 05:12:37 +08:00
|
|
|
def test_hello(capsys):
|
2009-02-27 18:18:27 +08:00
|
|
|
print 42
|
2009-05-21 05:12:37 +08:00
|
|
|
out, err = capsys.reset()
|
2009-02-27 18:18:27 +08:00
|
|
|
assert out.startswith("42")
|
|
|
|
""")
|
2009-05-20 01:25:21 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_stdfd_functional(self, testdir):
|
2009-05-20 01:25:21 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2009-05-21 05:12:37 +08:00
|
|
|
def test_hello(capfd):
|
2009-02-27 18:18:27 +08:00
|
|
|
import os
|
|
|
|
os.write(1, "42")
|
2009-05-21 05:12:37 +08:00
|
|
|
out, err = capfd.reset()
|
2009-02-27 18:18:27 +08:00
|
|
|
assert out.startswith("42")
|
|
|
|
""")
|
2009-05-20 01:25:21 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|