test_excinfo.py


Overview

[test_excinfo.py](/projects/286/67543) is a comprehensive test suite for the exception handling and traceback reporting functionality in the `pytest` framework, primarily focusing on the `_pytest._code` module's `ExceptionInfo`, `FormattedExcinfo`, and related classes. It validates that exceptions are correctly captured, represented, filtered, and formatted in various scenarios, including simple exceptions, chained exceptions, recursion errors, source code retrieval, and Python 3.11+ native `ExceptionGroup` support.

The file ensures that `pytest`'s mechanisms for introspecting exceptions, tracebacks, and formatting error output for terminal display work correctly and robustly across different Python versions and edge cases, such as missing source code or hidden traceback entries.


Key Classes and Functions

TestTraceback_f_g_h (Class)


TestGroupContains (Class)


TestFormattedExcinfo (Class)


Utility Functions and Fixtures


Important Implementation Details and Algorithms


Interaction with Other System Parts


Usage Examples

Capturing a simple exception:

def test_excinfo_simple() -> None:
    try:
        raise ValueError
    except ValueError:
        info = _pytest._code.ExceptionInfo.from_current()
    assert info.type == ValueError

Filtering a traceback hiding frames with __tracebackhide__:

def test_traceback_filter_selective():
    def f():
        raise ValueError

    def g():
        __tracebackhide__ = True
        f()

    def h():
        g()

    excinfo = pytest.raises(ValueError, h)
    filtered_tb = excinfo.traceback.filter(excinfo)
    # filtered_tb excludes frames marked with __tracebackhide__ = True

Checking if an exception group contains a specific exception type:

def test_group_contains():
    exc_group = ExceptionGroup("", [RuntimeError()])
    with pytest.raises(ExceptionGroup) as exc_info:
        raise exc_group
    assert exc_info.group_contains(RuntimeError)

Formatting exception info with locals and truncated args:

p = FormattedExcinfo(showlocals=True, truncate_args=True)
repr_excinfo = p.repr_excinfo(excinfo)
repr_excinfo.toterminal(tw_mock)

Mermaid Class Diagram

This diagram shows the main test classes and their key methods, reflecting the structure and focus areas of the test file.

classDiagram
    class TestTraceback_f_g_h {
        +setup_method(method)
        +test_traceback_entries()
        +test_traceback_entry_getsource()
        +test_traceback_entry_getsource_in_construct()
        +test_traceback_cut()
        +test_traceback_cut_excludepath(pytester)
        +test_traceback_filter()
        +test_traceback_filter_selective(tracebackhide, matching)
        +test_traceback_recursion_index()
        +test_traceback_only_specific_recursion_errors(monkeypatch)
        +test_traceback_no_recursion_index()
        +test_traceback_messy_recursion()
        +test_getreprcrash()
        +test_getreprcrash_empty()
    }

    class TestGroupContains {
        +test_contains_exception_type()
        +test_doesnt_contain_exception_type()
        +test_contains_exception_match()
        +test_doesnt_contain_exception_match()
        +test_contains_exception_type_unlimited_depth()
        +test_contains_exception_type_at_depth_1()
        +test_doesnt_contain_exception_type_past_depth()
        +test_contains_exception_type_specific_depth()
        +test_contains_exception_match_unlimited_depth()
        +test_contains_exception_match_at_depth_1()
        +test_doesnt_contain_exception_match_past_depth()
        +test_contains_exception_match_specific_depth()
    }

    class TestFormattedExcinfo {
        +test_repr_source()
        +test_repr_source_out_of_bounds()
        +test_repr_source_excinfo()
        +test_repr_source_not_existing()
        +test_repr_many_line_source_not_existing()
        +test_repr_source_failing_fullsource(monkeypatch)
        +test_repr_local()
        +test_repr_local_with_error()
        +test_repr_local_with_exception_in_class_property()
        +test_repr_local_truncated()
        +test_repr_args_not_truncated(importasmod)
        +test_repr_tracebackentry_lines(importasmod)
        +test_repr_tracebackentry_lines2(importasmod, tw_mock)
        +test_repr_tracebackentry_lines_var_kw_args(importasmod, tw_mock)
        +test_repr_tracebackentry_short(importasmod)
        +test_repr_traceback_entry_short_carets(importasmod)
        +test_repr_tracebackentry_no(importasmod)
        +test_repr_traceback_tbfilter(importasmod)
        +test_traceback_short_no_source(importasmod, monkeypatch)
        +test_repr_traceback_and_excinfo(importasmod)
        +test_repr_traceback_with_invalid_cwd(importasmod, monkeypatch)
        +test_repr_excinfo_addouterr(importasmod, tw_mock)
        +test_repr_excinfo_reprcrash(importasmod)
        +test_repr_traceback_recursion(importasmod)
        +test_reprexcinfo_getrepr(importasmod)
        +test_reprexcinfo_unicode()
        +test_toterminal_long(importasmod, tw_mock)
        +test_toterminal_long_missing_source(importasmod, tmp_path, tw_mock)
        +test_toterminal_long_incomplete_source(importasmod, tmp_path, tw_mock)
        +test_toterminal_long_filenames(importasmod, tw_mock, monkeypatch)
        +test_toterminal_value(importasmod, tw_mock)
        +test_format_excinfo(reproptions)
        +test_traceback_repr_style(importasmod, tw_mock)
        +test_exc_chain_repr(importasmod, tw_mock)
        +test_exc_repr_chain_suppression(importasmod, mode, tw_mock)
        +test_exc_chain_repr_without_traceback(importasmod, reason, description)
        +test_exc_chain_repr_cycle(importasmod, tw_mock)
        +test_exec_type_error_filter(importasmod)
    }

Summary

This file is critical for maintaining the quality and correctness of the pytest exception reporting mechanism.


End of documentation for test_excinfo.py