pytest
pytest copied to clipboard
prevent capteesys from duplicating output when -s disables global capture
Summary
Using the capteesys fixture with -s (--capture=no) printed output twice, even if the fixture wasn’t used in the test body. This issue is tracked in [pytest-dev/pytest#13784](https://github.com/pytest-dev/pytest/issues/13784) and is fixed by this PR.
Fixes: pytest-dev/pytest#13784
Root Cause
capteesys always instantiated a tee-ing SysCapture, so when global capture was disabled (-s), output went directly to the terminal and was also duplicated through the tee path.
Fix
Enable tee only when global capture is active. When global capture is disabled, capteesys yields a non-teeing fixture to avoid duplication.
Changes localized to: src/_pytest/capture.py (capteesys fixture)
Tests
Added regression test: test_capteesys_issue.py
- Reproduces duplication with
-sand validates the fix.
Behavior Comparison
Before:
Running pytest -s with capteesys in the test signature duplicated stdout/stderr.
After:
pytest -s prints each line exactly once (no duplication).
With capture enabled, capteesys continues to tee and capture as expected.
Backwards Compatibility
- No public API changes.
- Behavior now aligns with the selected global
--capturemode.
Affected Files
src/_pytest/capture.pytest_capteesys_issue.py