Skip to content

#1478 Add --show-capture option #3176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions _pytest/terminal.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,35 @@ def pytest_report_header(config, startdir):
str(testdir.tmpdir),
])

def test_show_capture(self, testdir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to test both (explicitly) and no.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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