test_debugging.py


Overview

`test_debugging.py` is a comprehensive test suite designed to validate the debugging capabilities integrated into the pytest testing framework. It focuses on testing the behavior and interaction of pytest's built-in debugger support (`--pdb` option), custom debugger classes, breakpoint management, and trace options.

The file contains numerous pytest-based tests that simulate various debugging scenarios, including post-mortem debugging on test failures, interaction with unittest, capturing of stdout/stderr and logs during debugging, recursive debugging sessions, custom debugger class integration, and breakpoint hook configuration.

This test suite also verifies the robustness of debugging features across different Python versions, ensures proper environment management, and validates the interaction between pytest's debugging hooks and other pytest components.


Detailed Documentation

Fixtures

pdb_env(request)


custom_pdb_calls()

def test_custom_pdb_usage(custom_pdb_calls):
    # After running tests that use --pdbcls=_pytest:_CustomPdb
    assert "init" in custom_pdb_calls

custom_debugger_hook()

def test_custom_debugger(custom_debugger_hook):
    # Run tests with --pdbcls=_pytest:_CustomDebugger
    assert "set_trace" in custom_debugger_hook

Functions

runpdb(pytester: Pytester, source: str) -> RunResult

result = runpdb(pytester, "def test_func(): assert False")

runpdb_and_get_stdout(pytester: Pytester, source: str) -> str

output = runpdb_and_get_stdout(pytester, "def test_func(): assert False")
print(output)

runpdb_and_get_report(pytester: Pytester, source: str) -> TestReport

report = runpdb_and_get_report(pytester, "def test_func(): assert False")
assert report.failed

Classes

TestPDB

pytest test_debugging.py::TestPDB::test_pdb_on_fail

TestDebuggingBreakpoints


TestTraceOption


Standalone Tests


Important Implementation Details


Interaction with Other System Components


Visual Diagram: Class Diagram of Main Test Classes

classDiagram
    class TestPDB {
        +pdblist(request)
        +test_pdb_on_fail(pytester, pdblist)
        +test_pdb_on_xfail(pytester, pdblist)
        +test_pdb_on_skip(pytester, pdblist)
        +test_pdb_unittest_postmortem(pytester)
        +test_pdb_print_captured_stdout_and_stderr(pytester)
        +test_pdb_set_trace_kwargs(pytester)
        +test_pdb_custom_cls(pytester, custom_pdb_calls)
        +... (many more test methods)
    }
    class TestDebuggingBreakpoints {
        +test_sys_breakpointhook_configure_and_unconfigure(pytester, arg)
        +test_pdb_custom_cls(pytester, custom_debugger_hook, monkeypatch)
        +test_environ_custom_class(pytester, custom_debugger_hook, arg)
        +test_sys_breakpoint_interception(pytester, monkeypatch)
        +test_pdb_not_altered(pytester)
    }
    class TestTraceOption {
        +test_trace_sets_breakpoint(pytester)
        +test_trace_with_parametrize_handles_shared_fixtureinfo(pytester)
    }

Summary

`test_debugging.py` serves as a vital integration and functional test suite verifying pytest's debugger integration features comprehensively. It tests standard and edge cases of PDB invocation, custom debugger class integration, breakpoint hook management, and debugger interaction during various test phases and failure modes. The tests ensure that pytest’s debugging capabilities behave reliably and predictably across Python versions and complex scenarios, maintaining pytest’s robustness as a testing framework with deep debugging support.