Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/doc/en/how-to/capture-stdout-stderr.rst

raw
1
2 .. _`captures`:
3
4 How to capture stdout/stderr output
5 =========================================================
6
7 Default stdout/stderr/stdin capturing behaviour
8 ---------------------------------------------------------
9
10 During test execution any output sent to ``stdout`` and ``stderr`` is
11 captured. If a test or a setup method fails its according captured
12 output will usually be shown along with the failure traceback. (this
13 behavior can be configured by the ``--show-capture`` command-line option).
14
15 In addition, ``stdin`` is set to a "null" object which will
16 fail on attempts to read from it because it is rarely desired
17 to wait for interactive input when running automated tests.
18
19 By default capturing is done by intercepting writes to low level
20 file descriptors. This allows to capture output from simple
21 print statements as well as output from a subprocess started by
22 a test.
23
24 .. _capture-method:
25
26 Setting capturing methods or disabling capturing
27 -------------------------------------------------
28
29 There are three ways in which ``pytest`` can perform capturing:
30
31 * ``fd`` (file descriptor) level capturing (default): All writes going to the
32 operating system file descriptors 1 and 2 will be captured.
33
34 * ``sys`` level capturing: Only writes to Python files ``sys.stdout``
35 and ``sys.stderr`` will be captured. No capturing of writes to
36 filedescriptors is performed.
37
38 * ``tee-sys`` capturing: Python writes to ``sys.stdout`` and ``sys.stderr``
39 will be captured, however the writes will also be passed-through to
40 the actual ``sys.stdout`` and ``sys.stderr``. This allows output to be
41 'live printed' and captured for plugin use, such as junitxml (new in pytest 5.4).
42
43 .. _`disable capturing`:
44
45 You can influence output capturing mechanisms from the command line:
46
47 .. code-block:: bash
48
49 pytest -s # disable all capturing
50 pytest --capture=sys # replace sys.stdout/stderr with in-mem files
51 pytest --capture=fd # also point filedescriptors 1 and 2 to temp file
52 pytest --capture=tee-sys # combines 'sys' and '-s', capturing sys.stdout/stderr
53 # and passing it along to the actual sys.stdout/stderr
54
55 .. _printdebugging:
56
57 Using print statements for debugging
58 ---------------------------------------------------
59
60 One primary benefit of the default capturing of stdout/stderr output
61 is that you can use print statements for debugging:
62
63 .. code-block:: python
64
65 # content of test_module.py
66
67
68 def setup_function(function):
69 print("setting up", function)
70
71
72 def test_func1():
73 assert True
74
75
76 def test_func2():
77 assert False
78
79 and running this module will show you precisely the output
80 of the failing function and hide the other one:
81
82 .. code-block:: pytest
83
84 $ pytest
85 =========================== test session starts ============================
86 platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
87 rootdir: /home/sweet/project
88 collected 2 items
89
90 test_module.py .F [100%]
91
92 ================================= FAILURES =================================
93 ________________________________ test_func2 ________________________________
94
95 def test_func2():
96 > assert False
97 E assert False
98
99 test_module.py:12: AssertionError
100 -------------------------- Captured stdout setup ---------------------------
101 setting up <function test_func2 at 0xdeadbeef0001>
102 ========================= short test summary info ==========================
103 FAILED test_module.py::test_func2 - assert False
104 ======================= 1 failed, 1 passed in 0.12s ========================
105
106 Accessing captured output from a test function
107 ---------------------------------------------------
108
109 The ``capsys``, ``capsysbinary``, ``capfd``, and ``capfdbinary`` fixtures
110 allow access to stdout/stderr output created during test execution. Here is
111 an example test function that performs some output related checks:
112
113 .. code-block:: python
114
115 def test_myoutput(capsys): # or use "capfd" for fd-level
116 print("hello")
117 sys.stderr.write("world\n")
118 captured = capsys.readouterr()
119 assert captured.out == "hello\n"
120 assert captured.err == "world\n"
121 print("next")
122 captured = capsys.readouterr()
123 assert captured.out == "next\n"
124
125 The ``readouterr()`` call snapshots the output so far -
126 and capturing will be continued. After the test
127 function finishes the original streams will
128 be restored. Using ``capsys`` this way frees your
129 test from having to care about setting/resetting
130 output streams and also interacts well with pytest's
131 own per-test capturing.
132
133 If you want to capture on filedescriptor level you can use
134 the ``capfd`` fixture which offers the exact
135 same interface but allows to also capture output from
136 libraries or subprocesses that directly write to operating
137 system level output streams (FD1 and FD2).
138
139
140
141 The return value from ``readouterr`` changed to a ``namedtuple`` with two attributes, ``out`` and ``err``.
142
143
144
145 If the code under test writes non-textual data, you can capture this using
146 the ``capsysbinary`` fixture which instead returns ``bytes`` from
147 the ``readouterr`` method.
148
149
150
151
152 If the code under test writes non-textual data, you can capture this using
153 the ``capfdbinary`` fixture which instead returns ``bytes`` from
154 the ``readouterr`` method. The ``capfdbinary`` fixture operates on the
155 filedescriptor level.
156
157
158
159
160 To temporarily disable capture within a test, both ``capsys``
161 and ``capfd`` have a ``disabled()`` method that can be used
162 as a context manager, disabling capture inside the ``with`` block:
163
164 .. code-block:: python
165
166 def test_disabling_capturing(capsys):
167 print("this output is captured")
168 with capsys.disabled():
169 print("output not captured, going directly to sys.stdout")
170 print("this output is also captured")