Skip to content
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

Adding stdio option for show-capture #11679

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS
Expand Up @@ -173,6 +173,7 @@ Ilya Konstantinov
Ionuț Turturică
Isaac Virshup
Israel Fruchter
Itamar Hindi
Itxaso Aizpurua
Iwan Briquemont
Jaap Broekhuizen
Expand Down Expand Up @@ -347,6 +348,7 @@ Sanket Duthade
Sankt Petersbug
Saravanan Padmanaban
Sean Malloy
Sebin (Eichel) Choi
Segev Finer
Serhii Mozghovyi
Seth Junot
Expand Down
1 change: 1 addition & 0 deletions changelog/11037.feature.rst
@@ -0,0 +1 @@
Added stdio as option for --show-capture to have both stderr and stdout output together.
8 changes: 7 additions & 1 deletion src/_pytest/debugging.py
Expand Up @@ -352,7 +352,13 @@ def _enter_pdb(
("stderr", rep.capstderr),
("log", rep.caplog),
):
if showcapture in (sectionname, "all") and content:
if (
showcapture in (sectionname, "all")
or (
showcapture == "stdio"
and ("stdout" in sectionname or "stderr" in sectionname)
)
) and content:
tw.sep(">", "captured " + sectionname)
if content[-1:] == "\n":
content = content[:-1]
Expand Down
22 changes: 18 additions & 4 deletions src/_pytest/terminal.py
Expand Up @@ -217,9 +217,9 @@ def pytest_addoption(parser: Parser) -> None:
"--show-capture",
action="store",
dest="showcapture",
choices=["no", "stdout", "stderr", "log", "all"],
choices=["no", "stdout", "stderr", "log", "stdio", "all"],
default="all",
help="Controls how captured stdout/stderr/log is shown on failed tests. "
help="Controls how captured stdout/stderr/log is shown on failed tests. stdio displays both stdout and stderr. "
"Default: all.",
)
group._addoption(
Expand Down Expand Up @@ -1039,7 +1039,14 @@ def print_teardown_sections(self, rep: TestReport) -> None:
if showcapture == "no":
return
for secname, content in rep.sections:
if showcapture != "all" and showcapture not in secname:
if (
showcapture != "all"
and showcapture not in secname
and (
showcapture != "stdio"
or ("stderr" not in secname and "stdout" not in secname)
)
):
continue
if "teardown" in secname:
self._tw.sep("-", secname)
Expand Down Expand Up @@ -1085,7 +1092,14 @@ def _outrep_summary(self, rep: BaseReport) -> None:
if showcapture == "no":
return
for secname, content in rep.sections:
if showcapture != "all" and showcapture not in secname:
if (
showcapture != "all"
and showcapture not in secname
and (
showcapture != "stdio"
or ("stderr" not in secname and "stdout" not in secname)
)
):
continue
self._tw.sep("-", secname)
if content[-1:] == "\n":
Expand Down
10 changes: 10 additions & 0 deletions testing/test_terminal.py
Expand Up @@ -1620,6 +1620,11 @@ def test_one():
assert "!This is stderr!" not in stdout
assert "!This is a warning log msg!" in stdout

stdout = pytester.runpytest("--show-capture=stdio", "--tb=short").stdout.str()
assert "!This is stdout!" in stdout
assert "!This is stderr!" in stdout
assert "!This is a warning log msg!" not in stdout

stdout = pytester.runpytest("--show-capture=no", "--tb=short").stdout.str()
assert "!This is stdout!" not in stdout
assert "!This is stderr!" not in stdout
Expand Down Expand Up @@ -1660,6 +1665,11 @@ def test_func():
assert "!stderr!" not in result
assert "!log!" in result

result = pytester.runpytest("--show-capture=stdio", "--tb=short").stdout.str()
assert "!stdout!" in result
assert "!stderr!" in result
assert "!log!" not in result

result = pytester.runpytest("--show-capture=no", "--tb=short").stdout.str()
assert "!stdout!" not in result
assert "!stderr!" not in result
Expand Down