How to handle test failures¶
Stopping after the first (or N) failures¶
To stop the testing process after the first (N) failures:
pytest -x # stop after first failure
pytest --maxfail=2 # stop after two failures
Using pdb — The Python Debugger with pytest¶
This will invoke the Python debugger on every failure (or KeyboardInterrupt). Often you might only want to do this for the first failing test to understand a certain failure situation:
pytest -x --pdb # drop to PDB on first failure, then end test session
pytest --pdb --maxfail=3 # drop to PDB for first three failures
Note that on any failure the exception information is stored on
interactive use, this allows one to drop into postmortem debugging with
any debug tool. One can also manually access the exception information,
>>> import sys
AssertionError('assert result == "ok"',)
pytest allows one to drop into the pdb prompt immediately at the start of each test via a command line option:
This will invoke the Python debugger at the start of every test.
To set a breakpoint in your code use the native Python
import pdb;pdb.set_trace() call
in your code and pytest automatically disables its output capture for that test:
Output capture in other tests is not affected.
Any prior test output that has already been captured and will be processed as such.
Output capture gets resumed when ending the debugger session (via the
Using the builtin breakpoint function¶
Python 3.7 introduces a builtin
Pytest supports the use of
breakpoint() with the following behaviours:
breakpoint()is called and
PYTHONBREAKPOINTis set to the default value, pytest will use the custom internal PDB trace UI instead of the system default
When tests are complete, the system will default back to the system
--pdbpassed to pytest, the custom internal Pdb trace UI is used with both
breakpoint()and failed tests/unhandled exceptions.
--pdbclscan be used to specify a custom debugger class.
New in version 5.0.
faulthandler standard module
can be used to dump Python tracebacks on a segfault or after a timeout.
The module is automatically enabled for pytest runs, unless the
-p no:faulthandler is given
on the command-line.
faulthandler_timeout=X configuration option can be used
to dump the traceback of all threads if a test takes longer than
seconds to finish (not available on Windows).
This functionality has been integrated from the external pytest-faulthandler plugin, with two small differences:
To disable it, use
-p no:faulthandlerinstead of
--no-faulthandler: the former can be used with any plugin, so it saves one option.
--faulthandler-timeoutcommand-line option has become the
faulthandler_timeoutconfiguration option. It can still be configured from the command-line using
Warning about unraisable exceptions and unhandled thread exceptions¶
New in version 6.2.
Unhandled exceptions are exceptions that are raised in a situation in which
they cannot propagate to a caller. The most common case is an exception raised
Unhandled thread exceptions are exceptions raised in a
but not handled, causing the thread to terminate uncleanly.
Both types of exceptions are normally considered bugs, but may go unnoticed because they don’t cause the program itself to crash. Pytest detects these conditions and issues a warning that is visible in the test run summary.
The plugins are automatically enabled for pytest runs, unless the
-p no:unraisableexception (for unraisable exceptions) and
-p no:threadexception (for thread exceptions) options are given on the
The warnings may be silenced selectively using the pytest.mark.filterwarnings
mark. The warning categories are