pytest icon indicating copy to clipboard operation
pytest copied to clipboard

prevent capteesys from duplicating output when -s disables global capture

Open ron-42 opened this issue 2 months ago • 1 comments

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 -s and 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 --capture mode.

Affected Files

  • src/_pytest/capture.py
  • test_capteesys_issue.py

ron-42 avatar Oct 07 '25 07:10 ron-42