diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 23fc10794..a75fce4c7 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -44,8 +44,9 @@ def pytest_addoption(parser): help="traceback print mode (auto/long/short/line/native/no).") group._addoption('--show-capture', action="store", dest="showcapture", - choices=['no', 'stdout', 'stderr'], - help="Print only stdout/stderr on not print both.") + 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).") @@ -629,8 +630,10 @@ class TerminalReporter: if self.config.option.showcapture == 'no': return for secname, content in rep.sections: - if self.config.option.showcapture and not (self.config.option.showcapture in secname): - continue + print(self.config.option.showcapture) + 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 index 316943e78..de6bd3118 100644 --- a/changelog/1478.feature +++ b/changelog/1478.feature @@ -1,4 +1 @@ -Added `--show-capture` feature. You can choose 'no', 'stdout' or 'stderr' option. -'no': stdout and stderr will now shown -'stdout': stdout will shown in terminal if you use this option but stderr will not shown. -'stderr': stderr will shown in terminal if you use this option but stdout will not shown. \ No newline at end of file +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/testing/test_terminal.py b/testing/test_terminal.py index b2dcf84a6..26739165d 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -836,6 +836,10 @@ def pytest_report_header(config, startdir): 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() @@ -844,6 +848,10 @@ def pytest_report_header(config, startdir): 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):