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
22 changes: 14 additions & 8 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,14 +1049,20 @@ def test_output(capteesys):
assert captured.out == "hello\n"
"""
capman: CaptureManager = request.config.pluginmanager.getplugin("capturemanager")
capture_fixture = CaptureFixture(
SysCapture, request, config=dict(tee=True), _ispytest=True
)
capman.set_fixture(capture_fixture)
capture_fixture._start()
yield capture_fixture
capture_fixture.close()
capman.unset_fixture()
if capman.is_globally_capturing():
capture_fixture = CaptureFixture(
SysCapture, request, config=dict(tee=True), _ispytest=True
)
capman.set_fixture(capture_fixture)
capture_fixture._start()
yield capture_fixture
capture_fixture.close()
capman.unset_fixture()
else:
# capteesys does nothing when global capturing is disabled.
# This is so that the "tee" part of cap-tee-sys is not
# implemented without the "cap" part.
yield CaptureFixture(SysCapture, request, _ispytest=True)


@fixture
Expand Down
24 changes: 24 additions & 0 deletions testing/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,30 @@ def test_one(capteesys):
result.stdout.fnmatch_lines(["sTdoUt", "sTdeRr"]) # no tee, just reported
assert not result.stderr.lines

def test_capteesys_no_global_capture(self, pytester: Pytester) -> None:
"""When global capture is disabled (-s), capteesys should not duplicate output.

It should pass output straight through (printed once) and capteesys.readouterr()
should return empty strings since no per-test capture is active.
"""
p = pytester.makepyfile(
"""\
import sys

def test_one(capteesys):
print("sTdoUt")
print("sTdeRr", file=sys.stderr)
out, err = capteesys.readouterr()
assert out == ""
assert err == ""
"""
)
# Run with -s to disable global capture; ensure each line appears exactly once
result = pytester.runpytest_subprocess(p, "-s", "--quiet", "--quiet")
assert result.ret == ExitCode.OK
assert result.stdout.str().count("sTdoUt") == 1
assert result.stderr.str().count("sTdeRr") == 1

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