Skip to content

Commit 3b521be

Browse files
Merge pull request #3841 from sankt-petersbug/fix-3816
Fix '--show-capture=no' capture teardown logs
2 parents 5b2c8fa + 672f4bb commit 3b521be

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

changelog/3816.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where ``--show-capture=no`` option would still show logs printed during fixture teardown.

src/_pytest/terminal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,12 @@ def summary_passes(self):
706706
self._outrep_summary(rep)
707707

708708
def print_teardown_sections(self, rep):
709+
showcapture = self.config.option.showcapture
710+
if showcapture == "no":
711+
return
709712
for secname, content in rep.sections:
713+
if showcapture != "all" and showcapture not in secname:
714+
continue
710715
if "teardown" in secname:
711716
self._tw.sep("-", secname)
712717
if content[-1:] == "\n":

testing/test_terminal.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,46 @@ def test_one():
948948
assert "!This is stderr!" not in stdout
949949
assert "!This is a warning log msg!" not in stdout
950950

951+
def test_show_capture_with_teardown_logs(self, testdir):
952+
"""Ensure that the capturing of teardown logs honor --show-capture setting"""
953+
testdir.makepyfile(
954+
"""
955+
import logging
956+
import sys
957+
import pytest
958+
959+
@pytest.fixture(scope="function", autouse="True")
960+
def hook_each_test(request):
961+
yield
962+
sys.stdout.write("!stdout!")
963+
sys.stderr.write("!stderr!")
964+
logging.warning("!log!")
965+
966+
def test_func():
967+
assert False
968+
"""
969+
)
970+
971+
result = testdir.runpytest("--show-capture=stdout", "--tb=short").stdout.str()
972+
assert "!stdout!" in result
973+
assert "!stderr!" not in result
974+
assert "!log!" not in result
975+
976+
result = testdir.runpytest("--show-capture=stderr", "--tb=short").stdout.str()
977+
assert "!stdout!" not in result
978+
assert "!stderr!" in result
979+
assert "!log!" not in result
980+
981+
result = testdir.runpytest("--show-capture=log", "--tb=short").stdout.str()
982+
assert "!stdout!" not in result
983+
assert "!stderr!" not in result
984+
assert "!log!" in result
985+
986+
result = testdir.runpytest("--show-capture=no", "--tb=short").stdout.str()
987+
assert "!stdout!" not in result
988+
assert "!stderr!" not in result
989+
assert "!log!" not in result
990+
951991

952992
@pytest.mark.xfail("not hasattr(os, 'dup')")
953993
def test_fdopen_kept_alive_issue124(testdir):

0 commit comments

Comments
 (0)