Merge pull request #3176 from feuillemorte/1478_no_stdout_option

#1478 Added --no-stdout option
This commit is contained in:
Bruno Oliveira 2018-02-09 18:36:36 -02:00 committed by GitHub
commit 063e2da967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 1 deletions

View File

@ -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 @@ class TerminalReporter(object):
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]

1
changelog/1478.feature Normal file
View File

@ -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).

View File

@ -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

View File

@ -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):