test_warning_types.py


Overview

The `test_warning_types.py` file contains automated tests for verifying the behavior and integration of warning classes defined in the `_pytest.warning_types` module. It ensures that all warning subclasses have their `__module__` attribute set properly to `"pytest"` (to present warnings as originating from `pytest` rather than internal modules), and that warning handling integrates correctly with pytest’s warning filters and reporting mechanisms.

The tests cover the following key aspects:

These tests help maintain the consistency and clarity of warning presentation in pytest, improving developer experience when dealing with warnings in test suites.


Classes and Functions

This file contains three test functions, each decorated with `pytest` marks to parameterize inputs or control warning filtering behavior.

1. test_warning_types(warning_class: UserWarning) -> None

Purpose

Ensures that all warning classes declared in `_pytest.warning_types` have their `__module__` attribute set to `"pytest"`. This allows warnings to appear as coming from `pytest` rather than internal modules, improving clarity in warning messages.

Parameters

Returns

Usage Example

# This test is executed automatically with pytest; no manual invocation required.

Implementation Details


2. test_pytest_warnings_repr_integration_test(pytester: Pytester) -> None

Purpose

Verifies that the manipulation of the `__module__` attribute on pytest warning classes properly affects how warnings are displayed in pytest's output. This is a small integration test to confirm that warnings raised during a test using `pytest.PytestWarning` appear with the correct module prefix.

Parameters

Returns

Usage Example

# This test runs automatically; it creates a test file dynamically that raises a PytestWarning,
# then asserts that the warning message is displayed with the "pytest" module prefix.

Implementation Details


3. test_warn_explicit_for_annotates_errors_with_location() -> None

Purpose

Tests that `warn_explicit_for` (a utility function in `_pytest.warning_types`) annotates warning errors with source code location information when raising warnings explicitly. This ensures that warnings raised in this way include file and line number details in their error messages.

Parameters

Returns

Usage Example

# Executed automatically as part of pytest runs.

Implementation Details


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class test_warning_types.py {
        +test_warning_types(warning_class: UserWarning) : None
        +test_pytest_warnings_repr_integration_test(pytester: Pytester) : None
        +test_warn_explicit_for_annotates_errors_with_location() : None
    }
    test_warning_types.py ..> warning_types
    test_warning_types.py ..> pytester
    test_warning_types.py ..> pytest
    test_warning_types.py ..> warnings

Summary

The `test_warning_types.py` file is a focused test module ensuring that pytest’s custom warning classes behave as expected with respect to module attribution and integration with pytest’s warning framework. It combines dynamic inspection, integration testing, and error annotation verification to maintain the quality and clarity of warning messages emitted during pytest test runs.