Breakpoint and Trace Options

Purpose

This subtopic enables users to control interactive debugging behavior in pytest through command line options, specifically `--pdb`, `--pdbcls`, and `--trace`. These options allow tests to automatically invoke the Python debugger (PDB) either immediately when running tests or upon encountering errors or interruptions. The goal is to provide flexible, customizable debugging entry points that integrate seamlessly with pytest's test execution flow.

Functionality

Key workflows and methods:

Code Snippet: Adding Options and Registering Plugins

def pytest_addoption(parser):
    group = parser.getgroup("general")
    group.addoption("--pdb", action="store_true", help="Start debugger on errors")
    group.addoption(
        "--pdbcls",
        metavar="modulename:classname",
        type=_validate_usepdb_cls,
        help="Custom debugger class for --pdb",
    )
    group.addoption("--trace", action="store_true", help="Break into debugger on each test")

def pytest_configure(config):
    if config.getvalue("trace"):
        config.pluginmanager.register(PdbTrace(), "pdbtrace")
    if config.getvalue("usepdb"):
        config.pluginmanager.register(PdbInvoke(), "pdbinvoke")

Code Snippet: Wrapping Test Function for --trace

def wrap_pytest_function_for_tracing(pyfuncitem):
    _pdb = pytestPDB._init_pdb("runcall")
    testfunction = pyfuncitem.obj

    @functools.wraps(testfunction)
    def wrapper(*args, **kwargs):
        func = functools.partial(testfunction, *args, **kwargs)
        _pdb.runcall(func)

    pyfuncitem.obj = wrapper

Integration

This subtopic extends the **Debugger Integration** parent topic by providing concrete user-facing command line options and their implementations to invoke debugging sessions during test runs. It complements the "PDB Invocation and Wrapping" subtopic by:

Together, these breakpoints and trace options integrate tightly with pytest's test execution machinery to enhance developer productivity through immediate and flexible debugging capabilities.

Diagram

The following flowchart illustrates the core decision and invocation flow when running tests with breakpoint and trace options enabled:

flowchart TD
    Start[Test Run Start]
    CheckTrace{--trace enabled?}
    WrapTest[Wrap test function to enter debugger at start]
    RunTest[Run test function]
    CheckError{Test failed or KeyboardInterrupt?}
    CheckPdb{--pdb enabled?}
    SuspendCapture[Suspend IO capturing]
    ShowCapture[Show captured stdout/stderr/log]
    EnterDebugger[Enter PDB post-mortem]
    Continue[Continue test execution]
    End[Test Run End]

    Start --> CheckTrace
    CheckTrace -- Yes --> WrapTest
    CheckTrace -- No --> RunTest
    WrapTest --> RunTest
    RunTest --> CheckError
    CheckError -- Yes --> CheckPdb
    CheckError -- No --> Continue
    CheckPdb -- Yes --> SuspendCapture
    CheckPdb -- No --> Continue
    SuspendCapture --> ShowCapture --> EnterDebugger --> Continue
    Continue --> End

This flowchart highlights how the command line options influence test execution by injecting debugger sessions either immediately (`--trace`) or conditionally on failures (`--pdb`), managing IO capture around these interactions.