Skip to content

Commit

Permalink
Merge pull request #3176 from feuillemorte/1478_no_stdout_option
Browse files Browse the repository at this point in the history
#1478 Added --no-stdout option
  • Loading branch information
nicoddemus committed Feb 9, 2018
2 parents bba258a + da5882c commit 063e2da
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions _pytest/terminal.py
Expand Up @@ -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).")
Expand Down Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions 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).
3 changes: 2 additions & 1 deletion doc/en/capture.rst
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions testing/test_terminal.py
Expand Up @@ -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):
Expand Down

0 comments on commit 063e2da

Please sign in to comment.