Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/13784.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``capteesys`` producing doubled output when used with ``--capture=no`` (``-s``).
9 changes: 8 additions & 1 deletion src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,14 @@ def _start(self) -> None:

def close(self) -> None:
if self._capture is not None:
out, err = self._capture.pop_outerr_to_orig()
if self._config.get("tee"):
# When tee is enabled, output was already written to the
# original stream in real-time by TeeCaptureIO. Using
# pop_outerr_to_orig() would write it a second time via
# writeorg(), causing doubled output (see #13784).
out, err = self._capture.readouterr()
else:
out, err = self._capture.pop_outerr_to_orig()
self._captured_out += out
self._captured_err += err
self._capture.stop_capturing()
Expand Down
20 changes: 20 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,26 @@ def test_one(capteesys):
result.stdout.fnmatch_lines(["sTdoUt", "sTdeRr"]) # no tee, just reported
assert not result.stderr.lines

def test_capteesys_no_double_output_with_capture_no(
self, pytester: Pytester
) -> None:
"""Capteesys with --capture=no should not produce doubled output (#13784)."""
p = pytester.makepyfile(
"""\
def test_one(capteesys):
print("hello world stdout")
out, err = capteesys.readouterr()
assert out == "hello world stdout\\n"
"""
)
result = pytester.runpytest(p, "--quiet", "--quiet", "-rN", "-s")
assert result.ret == ExitCode.OK
# "hello world stdout" should appear exactly once, not twice.
count = result.stdout.lines.count("hello world stdout")
assert count == 1, (
f"Expected 'hello world stdout' once, but found {count} times"
)

def test_capsyscapfd(self, pytester: Pytester) -> None:
p = pytester.makepyfile(
"""\
Expand Down