Resume output capturing after capsys/capfd.disabled() context manager
Fix #1993
This commit is contained in:
parent
22f338d74d
commit
f9589f7b64
|
@ -252,12 +252,14 @@ class CaptureFixture:
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def disabled(self):
|
def disabled(self):
|
||||||
|
self._capture.suspend_capturing()
|
||||||
capmanager = self.request.config.pluginmanager.getplugin('capturemanager')
|
capmanager = self.request.config.pluginmanager.getplugin('capturemanager')
|
||||||
capmanager.suspend_capture_item(self.request.node, "call", in_=True)
|
capmanager.suspend_global_capture(item=None, in_=False)
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
capmanager.resume_global_capture()
|
capmanager.resume_global_capture()
|
||||||
|
self._capture.resume_capturing()
|
||||||
|
|
||||||
|
|
||||||
def safe_text_dupfile(f, mode, default_encoding="UTF8"):
|
def safe_text_dupfile(f, mode, default_encoding="UTF8"):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Resume output capturing after ``capsys/capfd.disabled()`` context manager.
|
|
@ -502,20 +502,30 @@ class TestCaptureFixture(object):
|
||||||
assert 'closed' not in result.stderr.str()
|
assert 'closed' not in result.stderr.str()
|
||||||
|
|
||||||
@pytest.mark.parametrize('fixture', ['capsys', 'capfd'])
|
@pytest.mark.parametrize('fixture', ['capsys', 'capfd'])
|
||||||
def test_disabled_capture_fixture(self, testdir, fixture):
|
@pytest.mark.parametrize('no_capture', [True, False])
|
||||||
|
def test_disabled_capture_fixture(self, testdir, fixture, no_capture):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
def test_disabled({fixture}):
|
def test_disabled({fixture}):
|
||||||
print('captured before')
|
print('captured before')
|
||||||
with {fixture}.disabled():
|
with {fixture}.disabled():
|
||||||
print('while capture is disabled')
|
print('while capture is disabled')
|
||||||
print('captured after')
|
print('captured after')
|
||||||
|
assert {fixture}.readouterr() == ('captured before\\ncaptured after\\n', '')
|
||||||
|
|
||||||
|
def test_normal():
|
||||||
|
print('test_normal executed')
|
||||||
""".format(fixture=fixture))
|
""".format(fixture=fixture))
|
||||||
result = testdir.runpytest_subprocess()
|
args = ('-s',) if no_capture else ()
|
||||||
|
result = testdir.runpytest_subprocess(*args)
|
||||||
result.stdout.fnmatch_lines("""
|
result.stdout.fnmatch_lines("""
|
||||||
*while capture is disabled*
|
*while capture is disabled*
|
||||||
""")
|
""")
|
||||||
assert 'captured before' not in result.stdout.str()
|
assert 'captured before' not in result.stdout.str()
|
||||||
assert 'captured after' not in result.stdout.str()
|
assert 'captured after' not in result.stdout.str()
|
||||||
|
if no_capture:
|
||||||
|
assert 'test_normal executed' in result.stdout.str()
|
||||||
|
else:
|
||||||
|
assert 'test_normal executed' not in result.stdout.str()
|
||||||
|
|
||||||
@pytest.mark.parametrize('fixture', ['capsys', 'capfd'])
|
@pytest.mark.parametrize('fixture', ['capsys', 'capfd'])
|
||||||
def test_fixture_use_by_other_fixtures(self, testdir, fixture):
|
def test_fixture_use_by_other_fixtures(self, testdir, fixture):
|
||||||
|
|
Loading…
Reference in New Issue