From 223eef6261ef31393452694fcec257f8a389fd94 Mon Sep 17 00:00:00 2001 From: Sankt Petersbug Date: Mon, 20 Aug 2018 14:25:01 -0500 Subject: [PATCH] Fix '--show-capture=no' capture teardown logs Add a check before printing teardown logs. 'print_teardown_sections' method does not check '--show-capture' option value, and teardown logs are always printed. Resolves: #3816 --- changelog/3816.bugfix.rst | 1 + src/_pytest/terminal.py | 5 +++++ testing/test_terminal.py | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 changelog/3816.bugfix.rst diff --git a/changelog/3816.bugfix.rst b/changelog/3816.bugfix.rst new file mode 100644 index 000000000..6a399d598 --- /dev/null +++ b/changelog/3816.bugfix.rst @@ -0,0 +1 @@ +Fix ``--show-capture=no`` option still capture teardown logs. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 7dd2edd6f..f79624989 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -706,7 +706,12 @@ class TerminalReporter(object): self._outrep_summary(rep) def print_teardown_sections(self, rep): + showcapture = self.config.option.showcapture + if showcapture == "no": + return for secname, content in rep.sections: + if showcapture != "all" and showcapture not in secname: + continue if "teardown" in secname: self._tw.sep("-", secname) if content[-1:] == "\n": diff --git a/testing/test_terminal.py b/testing/test_terminal.py index a9da27980..88e5287e8 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -948,6 +948,46 @@ def pytest_report_header(config, startdir): assert "!This is stderr!" not in stdout assert "!This is a warning log msg!" not in stdout + def test_show_capture_with_teardown_logs(self, testdir): + """Ensure that the capturing of teardown logs honor --show-capture setting""" + testdir.makepyfile( + """ + import logging + import sys + import pytest + + @pytest.fixture(scope="function", autouse="True") + def hook_each_test(request): + yield + sys.stdout.write("!stdout!") + sys.stderr.write("!stderr!") + logging.warning("!log!") + + def test_func(): + assert False + """ + ) + + result = testdir.runpytest("--show-capture=stdout", "--tb=short").stdout.str() + assert "!stdout!" in result + assert "!stderr!" not in result + assert "!log!" not in result + + result = testdir.runpytest("--show-capture=stderr", "--tb=short").stdout.str() + assert "!stdout!" not in result + assert "!stderr!" in result + assert "!log!" not in result + + result = testdir.runpytest("--show-capture=log", "--tb=short").stdout.str() + assert "!stdout!" not in result + assert "!stderr!" not in result + assert "!log!" in result + + result = testdir.runpytest("--show-capture=no", "--tb=short").stdout.str() + assert "!stdout!" not in result + assert "!stderr!" not in result + assert "!log!" not in result + @pytest.mark.xfail("not hasattr(os, 'dup')") def test_fdopen_kept_alive_issue124(testdir):