_pytest.capture module¶
per-test stdout/stderr capturing mechanism.
-
CLOSE_STDIN= <CloseStdinType.CLOSE_STDIN: 1>¶ Sentinel to close stdin.
-
for ... in
pytest_load_initial_conftests(early_config: _pytest.config.Config)[source]¶
-
class
CaptureManager(method: _CaptureMethod)[source]¶ Bases:
objectCapture plugin, manages that the appropriate capture method is enabled/disabled during collection and each test phase (setup, call, teardown). After each of those points, the captured output is obtained and attached to the collection/runtest report.
There are two levels of capture: * global: which is enabled by default and can be suppressed by the
-soption. This is always enabled/disabledduring collection and each test phase.
fixture: when a test function or one of its fixture depend on the
capsysorcapfdfixtures. In this case special handling is needed to ensure the fixtures take precedence over the global capture.
-
with
_capturing_for_request(request: _pytest.fixtures.FixtureRequest) → Generator[CaptureFixture, None, None][source]¶ Context manager that creates a
CaptureFixtureinstance for the givenrequest, ensuring there is only a single one being requested at the same time.This is used as a helper with
capsys,capfdetc.
-
activate_fixture()[source]¶ If the current item is using
capsysorcapfd, activate them so they take precedence over the global capture.
-
for ... in
capsys(request)[source]¶ Enable text capturing of writes to
sys.stdoutandsys.stderr.The captured output is made available via
capsys.readouterr()method calls, which return a(out, err)namedtuple.outanderrwill betextobjects.
-
for ... in
capsysbinary(request)[source]¶ Enable bytes capturing of writes to
sys.stdoutandsys.stderr.The captured output is made available via
capsysbinary.readouterr()method calls, which return a(out, err)namedtuple.outanderrwill bebytesobjects.
-
for ... in
capfd(request)[source]¶ Enable text capturing of writes to file descriptors
1and2.The captured output is made available via
capfd.readouterr()method calls, which return a(out, err)namedtuple.outanderrwill betextobjects.
-
for ... in
capfdbinary(request)[source]¶ Enable bytes capturing of writes to file descriptors
1and2.The captured output is made available via
capfd.readouterr()method calls, which return a(out, err)namedtuple.outanderrwill bebyteobjects.
-
class
CaptureFixture(captureclass, request)[source]¶ Bases:
objectObject returned by
capsys(),capsysbinary(),capfd()andcapfdbinary()fixtures.
-
safe_text_dupfile(f, mode, default_encoding='UTF8')[source]¶ return an open text file object that’s a duplicate of f on the FD-level if possible.
-
class
EncodedFile(buffer: BinaryIO, encoding: str)[source]¶ Bases:
object-
errors= 'strict'¶
-
name¶ Ensure that file.name is a string.
-
mode¶
-
-
class
CaptureResult(out, err)¶ Bases:
tuple-
_asdict()¶ Return a new dict which maps field names to their values.
-
_field_defaults= {}¶
-
_fields= ('out', 'err')¶
-
_fields_defaults= {}¶
-
classmethod
_make(iterable)¶ Make a new CaptureResult object from a sequence or iterable
-
_replace(**kwds)¶ Return a new CaptureResult object replacing specified fields with new values
-
err¶ Alias for field number 1
-
out¶ Alias for field number 0
-
-
class
MultiCapture(out=True, err=True, in_=True, Capture=None)[source]¶ Bases:
object-
_state= None¶
-
_in_suspended= False¶
-
in_= None¶
-
out= None¶
-
err= None¶
-
-
class
FDCaptureBinary(targetfd, tmpfile=None)[source]¶ Bases:
objectCapture IO to/from a given os-level filedescriptor.
snap() produces
bytes-
EMPTY_BUFFER= b''¶
-
_state= None¶
-
-
class
FDCapture(targetfd, tmpfile=None)[source]¶ Bases:
_pytest.capture.FDCaptureBinaryCapture IO to/from a given os-level filedescriptor.
snap() produces text
-
EMPTY_BUFFER= ''¶
-
-
class
SysCaptureBinary(fd, tmpfile=None, stdin=<CloseStdinType.CLOSE_STDIN: 1>)[source]¶ Bases:
object-
CLOSE_STDIN= 1¶
-
EMPTY_BUFFER= b''¶
-
_state= None¶
-
-
class
SysCapture(fd, tmpfile=None, stdin=<CloseStdinType.CLOSE_STDIN: 1>)[source]¶ Bases:
_pytest.capture.SysCaptureBinary-
EMPTY_BUFFER= ''¶
-
-
class
TeeSysCapture(fd, tmpfile=None)[source]¶ Bases:
_pytest.capture.SysCapture
-
class
DontReadFromInput[source]¶ Bases:
object-
encoding= None¶
-
readline(*args)¶
-
readlines(*args)¶
-
buffer¶
-
-
_colorama_workaround()[source]¶ Ensure colorama is imported so that it attaches to the correct stdio handles on Windows.
colorama uses the terminal on import time. So if something does the first import of colorama while I/O capture is active, colorama will fail in various ways.
-
_readline_workaround()[source]¶ Ensure readline is imported so that it attaches to the correct stdio handles on Windows.
Pdb uses readline support where available–when not running from the Python prompt, the readline module is not imported until running the pdb REPL. If running pytest with the –pdb option this means the readline module is not imported until after I/O capture has been started.
This is a problem for pyreadline, which is often used to implement readline support on Windows, as it does not attach to the correct handles for stdout and/or stdin if they have been redirected by the FDCapture mechanism. This workaround ensures that readline is imported before I/O capture is setup so that it can attach to the actual stdin/out for the console.
-
_py36_windowsconsoleio_workaround(stream)[source]¶ Python 3.6 implemented unicode console handling for Windows. This works by reading/writing to the raw console handle using
{Read,Write}ConsoleW.The problem is that we are going to
dup2over the stdio file descriptors when doingFDCaptureand this willCloseHandlethe handles used by Python to write to the console. Though there is still some weirdness and the console handle seems to only be closed randomly and not on the first call toCloseHandle, or maybe it gets reopened with the same handle value when we suspend capturing.The workaround in this case will reopen stdio with a different fd which also means a different handle by replicating the logic in “Py_lifecycle.c:initstdio/create_stdio”.
- Parameters
stream – in practice
sys.stdoutorsys.stderr, but given here as parameter for unittesting purposes.