Skip to content

Commit f584444

Browse files
authored
Merge pull request #6442 from blueyed/rP
terminal: summary_passes: handle teardown sections
2 parents 4a265ba + 61d04d3 commit f584444

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

changelog/2780.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Captured output during teardown is shown with ``-rP``.

src/_pytest/terminal.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,20 @@ def summary_passes(self):
833833
msg = self._getfailureheadline(rep)
834834
self.write_sep("_", msg, green=True, bold=True)
835835
self._outrep_summary(rep)
836+
self._handle_teardown_sections(rep.nodeid)
836837

837-
def print_teardown_sections(self, rep):
838+
def _get_teardown_reports(self, nodeid: str) -> List[TestReport]:
839+
return [
840+
report
841+
for report in self.getreports("")
842+
if report.when == "teardown" and report.nodeid == nodeid
843+
]
844+
845+
def _handle_teardown_sections(self, nodeid: str) -> None:
846+
for report in self._get_teardown_reports(nodeid):
847+
self.print_teardown_sections(report)
848+
849+
def print_teardown_sections(self, rep: TestReport) -> None:
838850
showcapture = self.config.option.showcapture
839851
if showcapture == "no":
840852
return
@@ -858,17 +870,11 @@ def summary_failures(self):
858870
line = self._getcrashline(rep)
859871
self.write_line(line)
860872
else:
861-
teardown_sections = {}
862-
for report in self.getreports(""):
863-
if report.when == "teardown":
864-
teardown_sections.setdefault(report.nodeid, []).append(report)
865-
866873
for rep in reports:
867874
msg = self._getfailureheadline(rep)
868875
self.write_sep("_", msg, red=True, bold=True)
869876
self._outrep_summary(rep)
870-
for report in teardown_sections.get(rep.nodeid, []):
871-
self.print_teardown_sections(report)
877+
self._handle_teardown_sections(rep.nodeid)
872878

873879
def summary_errors(self):
874880
if self.config.option.tbstyle != "no":

testing/test_terminal.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,15 @@ def test_pass_reporting_on_fail(testdir):
790790
def test_pass_output_reporting(testdir):
791791
testdir.makepyfile(
792792
"""
793+
def setup_module():
794+
print("setup_module")
795+
796+
def teardown_module():
797+
print("teardown_module")
798+
793799
def test_pass_has_output():
794800
print("Four score and seven years ago...")
801+
795802
def test_pass_no_output():
796803
pass
797804
"""
@@ -806,8 +813,12 @@ def test_pass_no_output():
806813
[
807814
"*= PASSES =*",
808815
"*_ test_pass_has_output _*",
816+
"*- Captured stdout setup -*",
817+
"setup_module",
809818
"*- Captured stdout call -*",
810819
"Four score and seven years ago...",
820+
"*- Captured stdout teardown -*",
821+
"teardown_module",
811822
"*= short test summary info =*",
812823
"PASSED test_pass_output_reporting.py::test_pass_has_output",
813824
"PASSED test_pass_output_reporting.py::test_pass_no_output",

0 commit comments

Comments
 (0)