2009-02-27 18:18:27 +08:00
|
|
|
import py
|
|
|
|
|
|
|
|
class IocapturePlugin:
|
|
|
|
""" capture sys.stdout/sys.stderr / fd1/fd2. """
|
2009-04-15 00:30:26 +08:00
|
|
|
def pytest_funcarg__stdcapture(self, request):
|
2009-02-27 18:18:27 +08:00
|
|
|
capture = Capture(py.io.StdCapture)
|
2009-04-15 00:30:26 +08:00
|
|
|
request.addfinalizer(capture.finalize)
|
2009-02-27 18:18:27 +08:00
|
|
|
return capture
|
|
|
|
|
2009-04-15 00:30:26 +08:00
|
|
|
def pytest_funcarg__stdcapturefd(self, request):
|
2009-02-27 18:18:27 +08:00
|
|
|
capture = Capture(py.io.StdCaptureFD)
|
2009-04-15 00:30:26 +08:00
|
|
|
request.addfinalizer(capture.finalize)
|
2009-02-27 18:18:27 +08:00
|
|
|
return capture
|
|
|
|
|
|
|
|
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-08 00:01:53 +08:00
|
|
|
plugintester.hookcheck(IocapturePlugin)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TestCapture:
|
|
|
|
def test_std_functional(self, testdir):
|
|
|
|
testdir.plugins.append(IocapturePlugin())
|
|
|
|
evrec = testdir.inline_runsource("""
|
|
|
|
def test_hello(stdcapture):
|
|
|
|
print 42
|
|
|
|
out, err = stdcapture.reset()
|
|
|
|
assert out.startswith("42")
|
|
|
|
""")
|
2009-04-07 18:48:57 +08:00
|
|
|
evrec.assertoutcome(passed=1)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def test_stdfd_functional(self, testdir):
|
|
|
|
testdir.plugins.append(IocapturePlugin())
|
|
|
|
evrec = testdir.inline_runsource("""
|
|
|
|
def test_hello(stdcapturefd):
|
|
|
|
import os
|
|
|
|
os.write(1, "42")
|
|
|
|
out, err = stdcapturefd.reset()
|
|
|
|
assert out.startswith("42")
|
|
|
|
""")
|
2009-04-07 18:48:57 +08:00
|
|
|
evrec.assertoutcome(passed=1)
|