test_fixture.py


Overview

`test_fixture.py` is a test module primarily focused on validating the behavior and features of the `caplog` pytest fixture, which provides logging capture capabilities during testing. It contains a series of pytest test functions that verify how logging levels, disabling/enabling logging, filtering, and message capturing work in various scenarios.

The file also includes a cleanup fixture to ensure that global logging state is restored after tests, preventing side effects across tests due to Python's global logging configuration.

This module interacts deeply with pytest's logging plugin and the `caplog` fixture, as well as pytest's testing utilities such as `Pytester` for running isolated test sessions and inspecting outcomes.


Detailed Explanation of Entities

Fixtures

cleanup_disabled_logging()

@pytest.fixture(autouse=True)
def cleanup_disabled_logging() -> Iterator[None]:

Test Functions

All test functions use pytest's standard signature, often accepting fixtures like `caplog` (pytest's LogCaptureFixture) or `pytester` (pytest's testing helper).


test_fixture_help(pytester: Pytester) -> None


test_change_level(caplog: pytest.LogCaptureFixture) -> None

caplog.set_level(logging.INFO)
logger.info("info message")  # captured
logger.debug("debug message")  # ignored

test_change_level_logging_disabled(caplog: pytest.LogCaptureFixture) -> None


test_change_level_undo(pytester: Pytester) -> None


test_change_disabled_level_undo(pytester: Pytester) -> None


test_change_level_undoes_handler_level(pytester: Pytester) -> None


test_with_statement_at_level(caplog: pytest.LogCaptureFixture) -> None

with caplog.at_level(logging.INFO):
    logger.info("info")  # captured
    logger.debug("debug")  # not captured

test_with_statement_at_level_logging_disabled(caplog: pytest.LogCaptureFixture) -> None


test_with_statement_filtering(caplog: pytest.LogCaptureFixture) -> None


test_force_enable_logging_level_string(...)


test_log_access(caplog: pytest.LogCaptureFixture) -> None


test_messages(caplog: pytest.LogCaptureFixture) -> None


test_record_tuples(caplog: pytest.LogCaptureFixture) -> None


test_unicode(caplog: pytest.LogCaptureFixture) -> None


test_clear(caplog: pytest.LogCaptureFixture) -> None


logging_during_setup_and_teardown(caplog: pytest.LogCaptureFixture) -> Iterator[None]


test_captures_for_all_stages(caplog: pytest.LogCaptureFixture, logging_during_setup_and_teardown: None) -> None


test_clear_for_call_stage(caplog: pytest.LogCaptureFixture, logging_during_setup_and_teardown: None) -> None


test_ini_controls_global_log_level(pytester: Pytester) -> None


test_can_override_global_log_level(pytester: Pytester) -> None


test_captures_despite_exception(pytester: Pytester) -> None


test_log_report_captures_according_to_config_option_upon_failure(pytester: Pytester) -> None


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

The following Mermaid class diagram summarizes the main entities and their relationships in this test file, focusing on the fixtures and test functions and their usage of the `caplog` fixture and Python logging objects.

classDiagram
    class cleanup_disabled_logging {
        <<fixture>>
        +__call__(): Iterator[None]
    }

    class logging_during_setup_and_teardown {
        <<fixture>>
        +__call__(): Iterator[None]
    }

    class TestFilter {
        +filter(record: LogRecord): bool
    }

    class caplog {
        <<pytest Fixture>>
        +set_level(level: int | str, logger: Optional[str] = None)
        +at_level(level: int, logger: Optional[str] = None)
        +filtering(filter: logging.Filter)
        +clear()
        +records: List[LogRecord]
        +messages: List[str]
        +record_tuples: List[Tuple[str, int, str]]
        -_force_enable_logging(level, logger)
    }

    class logger {
        <<logging.Logger>>
        +debug(msg)
        +info(msg)
        +warning(msg)
        +error(msg)
        +critical(msg)
    }

    cleanup_disabled_logging ..> logging : "restores state"
    logging_during_setup_and_teardown ..> caplog : "uses"
    TestFilter ..> logging : "inherits"
    caplog ..> logger : "captures logs from"
    test_change_level ..> caplog : "uses"
    test_with_statement_at_level ..> caplog : "uses"
    test_with_statement_filtering ..> caplog : "uses filtering"

Summary

This file is a comprehensive test suite for pytest's `caplog` fixture that captures logging output during tests. It ensures robust and isolated logging capture behavior, correct handling of logging levels, filtering, and integration with pytest configuration. It also safeguards against global logging state pollution by resetting logging after tests. The tests verify both simple and complex scenarios including nested contexts, exceptions, and different test phases.


Example Usage Snippet

def test_logging_at_info_level(caplog):
    caplog.set_level(logging.INFO)
    logger.info("Info message")
    logger.debug("Debug message")  # should not be captured

    assert "Info message" in caplog.text
    assert "Debug message" not in caplog.text

def test_temporary_log_level(caplog):
    with caplog.at_level(logging.WARNING):
        logger.info("Info message")  # not captured
        logger.warning("Warning message")  # captured

    assert "Warning message" in caplog.text
    assert "Info message" not in caplog.text

End of Documentation for test_fixture.py