test_recwarn.py


Overview

`test_recwarn.py` is a comprehensive test suite designed to validate the warning capturing and handling capabilities within the `pytest` testing framework. This file specifically tests the `pytest.warns`, `pytest.deprecated_call`, and `WarningsRecorder` utilities, which facilitate the detection, inspection, and assertion of Python warnings (including subclasses and deprecation warnings) during test execution.

The tests cover scenarios such as:

This file ensures that pytest’s warning-related helpers behave correctly, improving test reliability and developer feedback when warnings occur.


Detailed Explanations

Imports and Setup


Functions

test_recwarn_stacklevel(recwarn: WarningsRecorder) -> None

test_recwarn_functional(pytester: Pytester) -> None

test_recwarn_captures_deprecation_warning(recwarn: WarningsRecorder) -> None


Class: TestSubclassWarningPop

Tests how warnings behave with subclass hierarchies when using `pop()` on recorded warnings.


Class: TestWarningsRecorderChecker

Tests internal workings of `WarningsRecorder` and related behavior.


Class: TestDeprecatedCall

Tests the `pytest.deprecated_call()` helper that asserts a deprecation warning is raised during a function call or code block.


Class: TestWarns

Extensive tests for `pytest.warns()`, which asserts that a piece of code emits specified warnings.


Standalone Functions for Edge Cases


Important Implementation Details and Algorithms


Interaction with Other Parts of the System

By verifying these interactions, this file helps ensure reliable and consistent warning handling across the pytest ecosystem.


Visual Diagram

classDiagram
    class TestSubclassWarningPop {
        +raise_warnings_from_list(_warnings: list[type[Warning]])
        +test_pop_finds_exact_match()
        +test_pop_raises_if_no_match()
        +test_pop_finds_best_inexact_match()
    }
    class TestWarningsRecorderChecker {
        +test_recording()
        +test_warn_stacklevel()
        +test_typechecking()
        +test_invalid_enter_exit()
    }
    class TestDeprecatedCall {
        +dep(i: int, j: int | None = None) -> int
        +dep_explicit(i: int) -> None
        +test_deprecated_call_raises()
        +test_deprecated_call()
        +test_deprecated_call_ret()
        +test_deprecated_call_preserves()
        +test_deprecated_explicit_call_raises()
        +test_deprecated_explicit_call()
        +test_deprecated_call_no_warning(mode)
        +test_deprecated_call_modes(warning_type, mode, call_f_first)
        +test_deprecated_call_specificity()
        +test_deprecated_call_supports_match()
    }
    class TestWarns {
        +test_check_callable()
        +test_several_messages()
        +test_function()
        +test_warning_tuple()
        +test_as_contextmanager()
        +test_record()
        +test_record_only()
        +test_record_only_none_type_error()
        +test_record_by_subclass()
        +test_double_test(pytester: Pytester)
        +test_match_regex()
        +test_one_from_multiple_warns()
        +test_none_of_multiple_warns()
        +test_can_capture_previously_warned()
        +test_warns_context_manager_with_kwargs()
        +test_re_emit_single()
        +test_re_emit_multiple()
        +test_re_emit_match_single()
        +test_re_emit_match_multiple()
        +test_re_emit_non_match_single()
        +test_catch_warning_within_raise()
        +test_skip_within_warns(pytester: Pytester)
        +test_fail_within_warns(pytester: Pytester)
        +test_exit_within_warns(pytester: Pytester)
        +test_keyboard_interrupt_within_warns(pytester: Pytester)
    }
    TestSubclassWarningPop --> Warning
    TestWarningsRecorderChecker --> WarningsRecorder
    TestDeprecatedCall --> pytest.deprecated_call
    TestWarns --> pytest.warns

Summary

`test_recwarn.py` is a critical test module that validates pytest's handling of Python warnings. It ensures that warnings are captured, matched, and asserted correctly in various complex scenarios, including subclass hierarchies, deprecation calls, and warning context managers. The file also tests the robustness of warning-related APIs regarding error conditions, message validation, and interaction with pytest's control flow mechanisms.

This test suite guarantees that pytest provides reliable and predictable warning assertion tools, which are essential for maintaining high-quality, warning-aware test suites in Python projects.