Debian Codegraph @

doc/en/how-to/failures.rst

raw
1 .. _how-to-handle-failures:
2
3 How to handle test failures
4 =============================
5
6 .. _maxfail:
7
8 Stopping after the first (or N) failures
9 ---------------------------------------------------
10
11 To stop the testing process after the first (N) failures:
12
13 .. code-block:: bash
14
15 pytest -x # stop after first failure
16 pytest --maxfail=2 # stop after two failures
17
18
19 .. _pdb-option:
20
21 Using :doc:`python:library/pdb` with pytest
22 -------------------------------------------
23
24 Dropping to :doc:`pdb <python:library/pdb>` on failures
25 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26
27 Python comes with a builtin Python debugger called :doc:`pdb <python:library/pdb>`. ``pytest``
28 allows one to drop into the :doc:`pdb <python:library/pdb>` prompt via a command line option:
29
30 .. code-block:: bash
31
32 pytest --pdb
33
34 This will invoke the Python debugger on every failure (or KeyboardInterrupt).
35 Often you might only want to do this for the first failing test to understand
36 a certain failure situation:
37
38 .. code-block:: bash
39
40 pytest -x --pdb # drop to PDB on first failure, then end test session
41 pytest --pdb --maxfail=3 # drop to PDB for first three failures
42
43 Note that on any failure the exception information is stored on
44 ``sys.last_value``, ``sys.last_type`` and ``sys.last_traceback``. In
45 interactive use, this allows one to drop into postmortem debugging with
46 any debug tool. One can also manually access the exception information,
47 for example::
48
49 >>> import sys
50 >>> sys.last_traceback.tb_lineno
51 42
52 >>> sys.last_value
53 AssertionError('assert result == "ok"',)
54
55
56 .. _trace-option:
57
58 Dropping to :doc:`pdb <python:library/pdb>` at the start of a test
59 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60
61 ``pytest`` allows one to drop into the :doc:`pdb <python:library/pdb>` prompt immediately at the start of each test via a command line option:
62
63 .. code-block:: bash
64
65 pytest --trace
66
67 This will invoke the Python debugger at the start of every test.
68
69 .. _breakpoints:
70
71 Setting breakpoints
72 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
74 .. versionadded: 2.4.0
75
76 To set a breakpoint in your code use the native Python ``import pdb;pdb.set_trace()`` call
77 in your code and pytest automatically disables its output capture for that test:
78
79 * Output capture in other tests is not affected.
80 * Any prior test output that has already been captured and will be processed as
81 such.
82 * Output capture gets resumed when ending the debugger session (via the
83 ``continue`` command).
84
85
86 .. _`breakpoint-builtin`:
87
88 Using the builtin breakpoint function
89 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90
91 Python 3.7 introduces a builtin ``breakpoint()`` function.
92 Pytest supports the use of ``breakpoint()`` with the following behaviours:
93
94 - When ``breakpoint()`` is called and ``PYTHONBREAKPOINT`` is set to the default value, pytest will use the custom internal PDB trace UI instead of the system default ``Pdb``.
95 - When tests are complete, the system will default back to the system ``Pdb`` trace UI.
96 - With :option:`--pdb` passed to pytest, the custom internal Pdb trace UI is used with both ``breakpoint()`` and failed tests/unhandled exceptions.
97 - :option:`--pdbcls` can be used to specify a custom debugger class.
98
99
100 .. _faulthandler:
101
102 Fault Handler
103 -------------
104
105 .. versionadded:: 5.0
106
107 The :mod:`faulthandler` standard module
108 can be used to dump Python tracebacks on a segfault or after a timeout.
109
110 The module is automatically enabled for pytest runs, unless the ``-p no:faulthandler`` is given
111 on the command-line.
112
113 Also the :confval:`faulthandler_timeout=X<faulthandler_timeout>` configuration option can be used
114 to dump the traceback of all threads if a test takes longer than ``X``
115 seconds to finish.
116
117 .. note::
118
119 This functionality has been integrated from the external
120 `pytest-faulthandler <https://github.com/pytest-dev/pytest-faulthandler>`__ plugin, with two
121 small differences:
122
123 * To disable it, use ``-p no:faulthandler`` instead of ``--no-faulthandler``: the former
124 can be used with any plugin, so it saves one option.
125
126 * The ``--faulthandler-timeout`` command-line option has become the
127 :confval:`faulthandler_timeout` configuration option. It can still be configured from
128 the command-line using ``-o faulthandler_timeout=X``.
129
130
131 .. _unraisable:
132
133 Warning about unraisable exceptions and unhandled thread exceptions
134 -------------------------------------------------------------------
135
136 .. versionadded:: 6.2
137
138 Unhandled exceptions are exceptions that are raised in a situation in which
139 they cannot propagate to a caller. The most common case is an exception raised
140 in a :meth:`__del__ <object.__del__>` implementation.
141
142 Unhandled thread exceptions are exceptions raised in a :class:`~threading.Thread`
143 but not handled, causing the thread to terminate uncleanly.
144
145 Both types of exceptions are normally considered bugs, but may go unnoticed
146 because they don't cause the program itself to crash. Pytest detects these
147 conditions and issues a warning that is visible in the test run summary.
148
149 The plugins are automatically enabled for pytest runs, unless the
150 ``-p no:unraisableexception`` (for unraisable exceptions) and
151 ``-p no:threadexception`` (for thread exceptions) options are given on the
152 command-line.
153
154 The warnings may be silenced selectively using the :ref:`pytest.mark.filterwarnings ref`
155 mark. The warning categories are :class:`pytest.PytestUnraisableExceptionWarning` and
156 :class:`pytest.PytestUnhandledThreadExceptionWarning`.