diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 51d21cb33f2..d37dd2c433a 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -42,6 +42,11 @@ def pytest_addoption(parser): action="store", dest="tbstyle", default='auto', choices=['auto', 'long', 'short', 'no', 'line', 'native'], help="traceback print mode (auto/long/short/line/native/no).") + group._addoption('--show-capture', + action="store", dest="showcapture", + choices=['no', 'stdout', 'stderr', 'both'], default='both', + help="Controls how captured stdout/stderr is shown on failed tests. " + "Default is 'both'.") group._addoption('--fulltrace', '--full-trace', action="store_true", default=False, help="don't cut any tracebacks (default is to cut).") @@ -622,7 +627,12 @@ def summary_errors(self): def _outrep_summary(self, rep): rep.toterminal(self._tw) + if self.config.option.showcapture == 'no': + return for secname, content in rep.sections: + if self.config.option.showcapture != 'both': + if not (self.config.option.showcapture in secname): + continue self._tw.sep("-", secname) if content[-1:] == "\n": content = content[:-1] diff --git a/changelog/1478.feature b/changelog/1478.feature new file mode 100644 index 00000000000..de6bd311890 --- /dev/null +++ b/changelog/1478.feature @@ -0,0 +1 @@ +New ``--show-capture`` command-line option that allows to specify how to display captured output when tests fail: ``no``, ``stdout``, ``stderr`` or ``both`` (the default). \ No newline at end of file diff --git a/doc/en/capture.rst b/doc/en/capture.rst index 3315065c529..901def6021c 100644 --- a/doc/en/capture.rst +++ b/doc/en/capture.rst @@ -9,7 +9,8 @@ Default stdout/stderr/stdin capturing behaviour During test execution any output sent to ``stdout`` and ``stderr`` is captured. If a test or a setup method fails its according captured -output will usually be shown along with the failure traceback. +output will usually be shown along with the failure traceback. (this +behavior can be configured by the ``--show-capture`` command-line option). In addition, ``stdin`` is set to a "null" object which will fail on attempts to read from it because it is rarely desired diff --git a/testing/test_terminal.py b/testing/test_terminal.py index d7fabd05589..e95a3ed2b6a 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -823,6 +823,35 @@ def pytest_report_header(config, startdir): str(testdir.tmpdir), ]) + def test_show_capture(self, testdir): + testdir.makepyfile(""" + import sys + def test_one(): + sys.stdout.write('!This is stdout!') + sys.stderr.write('!This is stderr!') + assert False, 'Something failed' + """) + + result = testdir.runpytest("--tb=short") + result.stdout.fnmatch_lines(["!This is stdout!"]) + result.stdout.fnmatch_lines(["!This is stderr!"]) + + result = testdir.runpytest("--show-capture=both", "--tb=short") + result.stdout.fnmatch_lines(["!This is stdout!"]) + result.stdout.fnmatch_lines(["!This is stderr!"]) + + result = testdir.runpytest("--show-capture=stdout", "--tb=short") + assert "!This is stderr!" not in result.stdout.str() + assert "!This is stdout!" in result.stdout.str() + + result = testdir.runpytest("--show-capture=stderr", "--tb=short") + assert "!This is stdout!" not in result.stdout.str() + assert "!This is stderr!" in result.stdout.str() + + result = testdir.runpytest("--show-capture=no", "--tb=short") + assert "!This is stdout!" not in result.stdout.str() + assert "!This is stderr!" not in result.stdout.str() + @pytest.mark.xfail("not hasattr(os, 'dup')") def test_fdopen_kept_alive_issue124(testdir):