From f9589f7b6487062474ba7a6af583e3360c2e6bae Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 29 Sep 2017 17:24:31 -0300 Subject: [PATCH] Resume output capturing after capsys/capfd.disabled() context manager Fix #1993 --- _pytest/capture.py | 4 +++- changelog/1993.bugfix | 1 + testing/test_capture.py | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 changelog/1993.bugfix diff --git a/_pytest/capture.py b/_pytest/capture.py index ff2a341dc..13e1216cc 100644 --- a/_pytest/capture.py +++ b/_pytest/capture.py @@ -252,12 +252,14 @@ class CaptureFixture: @contextlib.contextmanager def disabled(self): + self._capture.suspend_capturing() 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: yield finally: capmanager.resume_global_capture() + self._capture.resume_capturing() def safe_text_dupfile(f, mode, default_encoding="UTF8"): diff --git a/changelog/1993.bugfix b/changelog/1993.bugfix new file mode 100644 index 000000000..07a78cc91 --- /dev/null +++ b/changelog/1993.bugfix @@ -0,0 +1 @@ +Resume output capturing after ``capsys/capfd.disabled()`` context manager. diff --git a/testing/test_capture.py b/testing/test_capture.py index df5fc74f4..0fd012f7b 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -502,20 +502,30 @@ class TestCaptureFixture(object): assert 'closed' not in result.stderr.str() @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(""" def test_disabled({fixture}): print('captured before') with {fixture}.disabled(): print('while capture is disabled') print('captured after') + assert {fixture}.readouterr() == ('captured before\\ncaptured after\\n', '') + + def test_normal(): + print('test_normal executed') """.format(fixture=fixture)) - result = testdir.runpytest_subprocess() + args = ('-s',) if no_capture else () + result = testdir.runpytest_subprocess(*args) result.stdout.fnmatch_lines(""" *while capture is disabled* """) assert 'captured before' 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']) def test_fixture_use_by_other_fixtures(self, testdir, fixture):