failures.rst

Overview

This documentation file, **failures.rst**, is part of the pytest testing framework documentation. It provides comprehensive guidance on how to handle test failures effectively during test runs. The file covers various command-line options, debugging aids, and pytest features that help users detect, analyze, and respond to failing tests in an efficient manner.

The key topics include stopping test sessions after a certain number of failures, invoking interactive debugging sessions automatically on failures or at test starts, setting breakpoints in code, leveraging Python’s builtin breakpoint function, managing Python fault handling for crashes and timeouts, and warnings about unraisable exceptions in destructors or threads.

This file is intended for pytest users who want to improve their debugging experience and test failure handling, helping them identify problems faster and maintain test suite quality.


Detailed Explanations

Stopping after the first (or N) failures

**Purpose:** Allow users to abort the entire pytest run as soon as the first failure or a specified number of failures occur, to quickly focus on the immediate problems.

**Usage:**

pytest -x           # stop after the first failure
pytest --maxfail=2  # stop after two failures

This helps save time when many tests fail, allowing quick feedback on the most critical issues first.


Using pdb with pytest

**Purpose:** Integrate Python’s built-in debugger, `pdb`, to start an interactive debugging session automatically when a test fails or is interrupted.

**Usage:**

pytest --pdb                   # drop to pdb on every failure or KeyboardInterrupt
pytest -x --pdb                # drop to pdb on first failure then stop testing
pytest --pdb --maxfail=3       # drop to pdb on first three failures

**Important details:**

**Example:**

>>> import sys
>>> sys.last_traceback.tb_lineno
42
>>> sys.last_value
AssertionError('assert result == "ok"')

Dropping to pdb at the start of a test

**Purpose:** Invoke the debugger immediately as each test begins, allowing step-through debugging before any test code executes.

**Usage:**

pytest --trace

This is useful for diagnosing setup issues or complex test initialization sequences.


Setting breakpoints

**Introduced in pytest 2.4.0**

**Purpose:** Enable users to set breakpoints inside test code by calling `import pdb; pdb.set_trace()`.

**Behavior with pytest:**

This seamless integration improves the debugging experience without disrupting the overall test output handling.


Using the builtin breakpoint() function

**Context:** Python 3.7+ introduced a builtin `breakpoint()` function for entering debuggers.

**pytest support:**

This integration modernizes debugging support and aligns with Python’s evolving debugging interfaces.


Fault Handler

**Introduced in pytest 5.0**

**Purpose:** Automatically dump Python tracebacks on fatal errors like segmentation faults or after test timeouts, using the standard `faulthandler` module.

**Features:**

This feature aids in diagnosing crashes and hangs by providing immediate traceback information.


Warning about unraisable exceptions and unhandled thread exceptions

**Introduced in pytest 6.2**

**Purpose:** Detect and warn about exceptions that cannot propagate normally, such as those in `__del__` methods or unhandled exceptions in threads.

**Details:**

This proactive warning system helps maintain test and application robustness by exposing subtle bugs.


Implementation Details and Algorithms


Interaction with Other System Components

This file acts as a user-facing guide to these integrations rather than containing the actual implementation code.


Usage Examples Summary

Stop testing after first failure:

pytest -x

Invoke debugger on each failure:

pytest --pdb

Drop into debugger at test start:

pytest --trace

Set breakpoint in code:

import pdb; pdb.set_trace()

Use Python 3.7+ breakpoint function with pytest’s internal debugger:

breakpoint()

Enable fault handler with timeout dumping:

pytest -o faulthandler_timeout=10

Visual Diagram: pytest Failure Handling Features Structure

classDiagram
    class FailureHandling {
        +stop_after_failures(maxfail: int)
        +invoke_pdb_on_failure()
        +invoke_pdb_at_test_start()
        +set_breakpoint_in_code()
        +use_builtin_breakpoint()
        +enable_fault_handler(timeout: int)
        +warn_unraisable_exceptions()
    }

    FailureHandling : -maxfail
    FailureHandling : -pdb_enabled
    FailureHandling : -trace_enabled
    FailureHandling : -fault_handler_enabled
    FailureHandling : -unraisable_exception_warnings_enabled

    FailureHandling ..> "pdb (Python Debugger)"
    FailureHandling ..> "faulthandler (Python Module)"
    FailureHandling ..> "Python Exception System"

Summary

The **failures.rst** documentation file is a comprehensive reference that enables pytest users to handle test failures proactively and efficiently. By leveraging built-in and pytest-integrated debugging tools, fault handlers, and informative warnings, users can quickly identify issues and maintain high test suite reliability. The features documented here enhance the developer experience and are tightly integrated with pytest’s core and Python’s standard debugging ecosystem.