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

Add capteesys capture fixture to bubble up output to --capture handler #12854

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
12 changes: 4 additions & 8 deletions doc/en/how-to/capture-stdout-stderr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
How to capture stdout/stderr output
=========================================================

Pytest can intercept stdout and stderr. Using the `--capture=` command-line
argument (described below) allows pytest to add captured output to reports
generated after tests. The reports can customized by `-r` (described below).

Built-in fixtures (also described below) can be passed to tests as arguments
to capture stdout and stderr and make it accessible to the developer during
the test for inspection. It will not be available for reports if it is captured,
unless it is re-printed with capturing turned off (see below).
Pytest can intercept stdout and stderr by using the ``--capture=`` command-line
or argument or by using fixtures. The flag ``--capture=`` configures reporting,
whereas the fixtures offer somewhat more granular control and allow inspection
during testing. The reports can customized with the `-r flag <../reference/reference.html#command-line-flags>`_.
ayjayt marked this conversation as resolved.
Show resolved Hide resolved

ayjayt marked this conversation as resolved.
Show resolved Hide resolved
Default stdout/stderr/stdin capturing behaviour
---------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions doc/en/reference/fixtures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Built-in fixtures
:fixture:`capsys`
Capture, as text, output to ``sys.stdout`` and ``sys.stderr``.

:fixture:`capteesys`
Capture in the same manner as :fixture:`capsys`, but also pass text
through according to ``--capture=``.

:fixture:`capsysbinary`
Capture, as bytes, output to ``sys.stdout`` and ``sys.stderr``.

Expand Down
10 changes: 10 additions & 0 deletions doc/en/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ capsys
.. autoclass:: pytest.CaptureFixture()
:members:

.. fixture:: capteesys

capteesys
~~~~~~~~~

**Tutorial**: :ref:`captures`

.. autofunction:: _pytest.capture.capteesys()
:no-auto-options:

.. fixture:: capsysbinary

capsysbinary
Expand Down
5 changes: 3 additions & 2 deletions src/_pytest/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,14 +1007,15 @@ def test_output(capsys):
@fixture
def capteesys(request: SubRequest) -> Generator[CaptureFixture[str]]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fixture is almost 100% a copy of the capsys one above it, since a tee-sys is just a sys w/ an extra argument (tree=True)

r"""Enable simultaneous text capturing and pass-through of writes
to ``sys.stdout`` and ``sys.stderr``.
to ``sys.stdout`` and ``sys.stderr`` as defined by ``--capture=``.


The captured output is made available via ``capteesys.readouterr()`` method
calls, which return a ``(out, err)`` namedtuple.
``out`` and ``err`` will be ``text`` objects.

The output is also passed-through, allowing it to be "live-printed".
The output is also passed-through, allowing it to be "live-printed",
reported, or both as defined by ``--capture=``.

Returns an instance of :class:`CaptureFixture[str] <pytest.CaptureFixture>`.

Expand Down