Skip to content

Commit

Permalink
Add disabled() method to capsys and capfd
Browse files Browse the repository at this point in the history
Fix #1599
  • Loading branch information
nicoddemus committed Jun 9, 2016
1 parent 9232389 commit 4a3d57f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
23 changes: 17 additions & 6 deletions _pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from __future__ import with_statement

import contextlib
import sys
import os
from tempfile import TemporaryFile
Expand Down Expand Up @@ -146,8 +147,8 @@ def pytest_keyboard_interrupt(self, excinfo):
def pytest_internalerror(self, excinfo):
self.reset_capturings()

def suspendcapture_item(self, item, when):
out, err = self.suspendcapture()
def suspendcapture_item(self, item, when, in_=False):
out, err = self.suspendcapture(in_=in_)
item.add_report_section(when, "stdout", out)
item.add_report_section(when, "stderr", err)

Expand All @@ -162,7 +163,7 @@ def capsys(request):
"""
if "capfd" in request._funcargs:
raise request.raiseerror(error_capsysfderror)
request.node._capfuncarg = c = CaptureFixture(SysCapture)
request.node._capfuncarg = c = CaptureFixture(SysCapture, request)
return c

@pytest.fixture
Expand All @@ -175,17 +176,18 @@ def capfd(request):
request.raiseerror(error_capsysfderror)
if not hasattr(os, 'dup'):
pytest.skip("capfd funcarg needs os.dup")
request.node._capfuncarg = c = CaptureFixture(FDCapture)
request.node._capfuncarg = c = CaptureFixture(FDCapture, request)
return c


class CaptureFixture:
def __init__(self, captureclass):
def __init__(self, captureclass, request):
self.captureclass = captureclass
self.request = request

def _start(self):
self._capture = MultiCapture(out=True, err=True, in_=False,
Capture=self.captureclass)
Capture=self.captureclass)
self._capture.start_capturing()

def close(self):
Expand All @@ -200,6 +202,15 @@ def readouterr(self):
except AttributeError:
return self._outerr

@contextlib.contextmanager
def disabled(self):
capmanager = self.request.config.pluginmanager.getplugin('capturemanager')
capmanager.suspendcapture_item(self.request.node, "call", in_=True)
try:
yield
finally:
capmanager.resumecapture()


def safe_text_dupfile(f, mode, default_encoding="UTF8"):
""" return a open text file object that's a duplicate of f on the
Expand Down
16 changes: 16 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,22 @@ def test_log(capsys):
result = testdir.runpytest_subprocess(p)
assert 'closed' not in result.stderr.str()

@pytest.mark.parametrize('fixture', ['capsys', 'capfd'])
def test_disabled_capture_fixture(self, testdir, fixture):
testdir.makepyfile("""
def test_disabled({fixture}):
print('captured before')
with {fixture}.disabled():
print('while capture is disabled')
print('captured after')
""".format(fixture=fixture))
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines("""
*while capture is disabled*
""")
assert 'captured before' not in result.stdout.str()
assert 'captured after' not in result.stdout.str()


def test_setup_failure_does_not_kill_capturing(testdir):
sub1 = testdir.mkpydir("sub1")
Expand Down

0 comments on commit 4a3d57f

Please sign in to comment.