diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 7f42f1284..23fc10794 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -42,9 +42,10 @@ 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('--no-stdout', - action="store_false", dest="nostdout", - help="Do not print stdout") + group._addoption('--show-capture', + action="store", dest="showcapture", + choices=['no', 'stdout', 'stderr'], + help="Print only stdout/stderr on not print both.") group._addoption('--fulltrace', '--full-trace', action="store_true", default=False, help="don't cut any tracebacks (default is to cut).") @@ -625,8 +626,10 @@ class TerminalReporter: def _outrep_summary(self, rep): rep.toterminal(self._tw) + if self.config.option.showcapture == 'no': + return for secname, content in rep.sections: - if not self.config.option.nostdout and 'stdout' in secname: + if self.config.option.showcapture and not (self.config.option.showcapture in secname): continue self._tw.sep("-", secname) if content[-1:] == "\n": diff --git a/changelog/1478.feature b/changelog/1478.feature new file mode 100644 index 000000000..316943e78 --- /dev/null +++ b/changelog/1478.feature @@ -0,0 +1,4 @@ +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 diff --git a/changelog/1478.trivial b/changelog/1478.trivial deleted file mode 100644 index aa88151e7..000000000 --- a/changelog/1478.trivial +++ /dev/null @@ -1 +0,0 @@ -Added `--no-stdout` feature. Stdout will not shown in terminal if you use this option. Only Stderr will shown. \ No newline at end of file diff --git a/testing/test_terminal.py b/testing/test_terminal.py index bedd6a9a5..b2dcf84a6 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -823,18 +823,26 @@ def pytest_report_header(config, startdir): str(testdir.tmpdir), ]) - def test_no_stdout(self, testdir): + def test_show_capture(self, testdir): testdir.makepyfile(""" + import sys def test_one(): - print('!This is stdout!') + 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("--no-stdout", "--tb=short") + 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() @pytest.mark.xfail("not hasattr(os, 'dup')")