Skip to content

Commit 6f93ffb

Browse files
author
Mathieu Clabaut
committed
Report teardown output on test failure
Until now, teardown stdout/stderr output was not reported upon test failure. However such output is sometime necessary to understand the failure. fix #442
1 parent 35d154f commit 6f93ffb

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Markus Unterwaditzer
9090
Martijn Faassen
9191
Martin K. Scherer
9292
Martin Prusse
93+
Mathieu Clabaut
9394
Matt Bachmann
9495
Matt Williams
9596
Matthias Hafner

CHANGELOG.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
or implicitly as a plugin (`#2005`_).
1818
Thanks `@RonnyPfannschmidt`_ for the report and `@nicoddemus`_ for the PR.
1919

20-
*
20+
* Report teardown output on test failure (`#442`_).
21+
Thanks `@matclab`_ or the PR.
2122

2223
*
2324

@@ -26,7 +27,9 @@
2627

2728
.. _@cwitty: https://github.com/cwitty
2829
.. _@okulynyak: https://github.com/okulynyak
30+
.. _@matclab: https://github.com/matclab
2931

32+
.. _#442: https://github.com/pytest-dev/pytest/issues/442
3033
.. _#1976: https://github.com/pytest-dev/pytest/issues/1976
3134
.. _#1998: https://github.com/pytest-dev/pytest/issues/1998
3235
.. _#2004: https://github.com/pytest-dev/pytest/issues/2004

_pytest/terminal.py

+12
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,15 @@ def summary_passes(self):
458458
self.write_sep("_", msg)
459459
self._outrep_summary(rep)
460460

461+
def print_teardown_sections(self, rep):
462+
for secname, content in rep.sections:
463+
if 'teardown' in secname:
464+
self._tw.sep('-', secname)
465+
if content[-1:] == "\n":
466+
content = content[:-1]
467+
self._tw.line(content)
468+
469+
461470
def summary_failures(self):
462471
if self.config.option.tbstyle != "no":
463472
reports = self.getreports('failed')
@@ -473,6 +482,9 @@ def summary_failures(self):
473482
markup = {'red': True, 'bold': True}
474483
self.write_sep("_", msg, **markup)
475484
self._outrep_summary(rep)
485+
for report in self.getreports(''):
486+
if report.nodeid == rep.nodeid and report.when == 'teardown':
487+
self.print_teardown_sections(report)
476488

477489
def summary_errors(self):
478490
if self.config.option.tbstyle != "no":

testing/test_terminal.py

+25
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,31 @@ def teardown_function(function):
370370
"*1 failed*1 error*",
371371
])
372372

373+
def test_setup_teardown_output_and_test_failure(self, testdir):
374+
""" Test for issue #442 """
375+
testdir.makepyfile("""
376+
def setup_function(function):
377+
print ("setup func")
378+
379+
def test_fail():
380+
assert 0, "failingfunc"
381+
382+
def teardown_function(function):
383+
print ("teardown func")
384+
""")
385+
result = testdir.runpytest()
386+
result.stdout.fnmatch_lines([
387+
"*test_fail*",
388+
"*def test_fail():",
389+
"*failingfunc*",
390+
"*Captured stdout setup*",
391+
"*setup func*",
392+
"*Captured stdout teardown*",
393+
"*teardown func*",
394+
395+
"*1 failed*",
396+
])
397+
373398
class TestTerminalFunctional:
374399
def test_deselected(self, testdir):
375400
testpath = testdir.makepyfile("""

0 commit comments

Comments
 (0)