From 949a620d3a182d469f8c6bb14498e5ec6802c6e6 Mon Sep 17 00:00:00 2001 From: feuillemorte Date: Wed, 31 Jan 2018 22:36:28 +0300 Subject: [PATCH 1/4] #1478 Added --no-stdout option --- _pytest/terminal.py | 5 +++++ changelog/1478.trivial | 1 + testing/test_terminal.py | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 changelog/1478.trivial diff --git a/_pytest/terminal.py b/_pytest/terminal.py index f0a2fa6187e..7f42f12840b 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -42,6 +42,9 @@ 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('--fulltrace', '--full-trace', action="store_true", default=False, help="don't cut any tracebacks (default is to cut).") @@ -623,6 +626,8 @@ def summary_errors(self): def _outrep_summary(self, rep): rep.toterminal(self._tw) for secname, content in rep.sections: + if not self.config.option.nostdout and 'stdout' in secname: + continue self._tw.sep("-", secname) if content[-1:] == "\n": content = content[:-1] diff --git a/changelog/1478.trivial b/changelog/1478.trivial new file mode 100644 index 00000000000..aa88151e7c4 --- /dev/null +++ b/changelog/1478.trivial @@ -0,0 +1 @@ +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 7dfa4b01efd..bedd6a9a549 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -823,6 +823,19 @@ def pytest_report_header(config, startdir): str(testdir.tmpdir), ]) + def test_no_stdout(self, testdir): + testdir.makepyfile(""" + def test_one(): + print('!This is stdout!') + assert False, 'Something failed' + """) + + result = testdir.runpytest("--tb=short") + result.stdout.fnmatch_lines(["!This is stdout!"]) + + result = testdir.runpytest("--no-stdout", "--tb=short") + assert "!This is stdout!" not in result.stdout.str() + @pytest.mark.xfail("not hasattr(os, 'dup')") def test_fdopen_kept_alive_issue124(testdir): From 1a650a9eb9b301fd2f47f627868fbabcfb65f37e Mon Sep 17 00:00:00 2001 From: feuillemorte Date: Tue, 6 Feb 2018 23:38:51 +0300 Subject: [PATCH 2/4] #1478 Added --show-capture option --- _pytest/terminal.py | 11 +++++++---- changelog/1478.feature | 4 ++++ changelog/1478.trivial | 1 - testing/test_terminal.py | 14 +++++++++++--- 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 changelog/1478.feature delete mode 100644 changelog/1478.trivial diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 7f42f12840b..23fc10794a0 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 @@ 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 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 00000000000..316943e782a --- /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 aa88151e7c4..00000000000 --- 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 bedd6a9a549..b2dcf84a6d0 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')") From 71367881ed14eb1cbdfee47a0cf8e0aa431f161c Mon Sep 17 00:00:00 2001 From: feuillemorte Date: Thu, 8 Feb 2018 16:21:22 +0300 Subject: [PATCH 3/4] #1478 Added --show-capture=both option (fix comments) --- _pytest/terminal.py | 11 +++++++---- changelog/1478.feature | 5 +---- testing/test_terminal.py | 8 ++++++++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/_pytest/terminal.py b/_pytest/terminal.py index 23fc10794a0..a75fce4c743 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 @@ def _outrep_summary(self, rep): 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 316943e782a..de6bd311890 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 b2dcf84a6d0..26739165d3f 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -836,6 +836,10 @@ def test_one(): 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 test_one(): 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): From da5882c2d5607174128f4618020a1ff26adc8316 Mon Sep 17 00:00:00 2001 From: feuillemorte Date: Fri, 9 Feb 2018 21:36:48 +0300 Subject: [PATCH 4/4] #1478 Add doc and remove print --- _pytest/terminal.py | 1 - doc/en/capture.rst | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_pytest/terminal.py b/_pytest/terminal.py index a75fce4c743..257fa87f165 100644 --- a/_pytest/terminal.py +++ b/_pytest/terminal.py @@ -630,7 +630,6 @@ def _outrep_summary(self, rep): if self.config.option.showcapture == 'no': return for secname, content in rep.sections: - print(self.config.option.showcapture) if self.config.option.showcapture != 'both': if not (self.config.option.showcapture in secname): continue 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