diff --git a/changelog/2780.bugfix.rst b/changelog/2780.bugfix.rst new file mode 100644 index 00000000000..d1d7e9914c1 --- /dev/null +++ b/changelog/2780.bugfix.rst @@ -0,0 +1 @@ +Captured output during teardown is shown with ``-rP``. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index e88545eca9c..804d5928f31 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -833,8 +833,20 @@ def summary_passes(self): msg = self._getfailureheadline(rep) self.write_sep("_", msg, green=True, bold=True) self._outrep_summary(rep) + self._handle_teardown_sections(rep.nodeid) - def print_teardown_sections(self, rep): + def _get_teardown_reports(self, nodeid: str) -> List[TestReport]: + return [ + report + for report in self.getreports("") + if report.when == "teardown" and report.nodeid == nodeid + ] + + def _handle_teardown_sections(self, nodeid: str) -> None: + for report in self._get_teardown_reports(nodeid): + self.print_teardown_sections(report) + + def print_teardown_sections(self, rep: TestReport) -> None: showcapture = self.config.option.showcapture if showcapture == "no": return @@ -858,17 +870,11 @@ def summary_failures(self): line = self._getcrashline(rep) self.write_line(line) else: - teardown_sections = {} - for report in self.getreports(""): - if report.when == "teardown": - teardown_sections.setdefault(report.nodeid, []).append(report) - for rep in reports: msg = self._getfailureheadline(rep) self.write_sep("_", msg, red=True, bold=True) self._outrep_summary(rep) - for report in teardown_sections.get(rep.nodeid, []): - self.print_teardown_sections(report) + self._handle_teardown_sections(rep.nodeid) def summary_errors(self): if self.config.option.tbstyle != "no": diff --git a/testing/test_terminal.py b/testing/test_terminal.py index d31033197de..fa45c48da36 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -790,8 +790,15 @@ def test_pass_reporting_on_fail(testdir): def test_pass_output_reporting(testdir): testdir.makepyfile( """ + def setup_module(): + print("setup_module") + + def teardown_module(): + print("teardown_module") + def test_pass_has_output(): print("Four score and seven years ago...") + def test_pass_no_output(): pass """ @@ -806,8 +813,12 @@ def test_pass_no_output(): [ "*= PASSES =*", "*_ test_pass_has_output _*", + "*- Captured stdout setup -*", + "setup_module", "*- Captured stdout call -*", "Four score and seven years ago...", + "*- Captured stdout teardown -*", + "teardown_module", "*= short test summary info =*", "PASSED test_pass_output_reporting.py::test_pass_has_output", "PASSED test_pass_output_reporting.py::test_pass_no_output",