test_formatter.py

Overview

`test_formatter.py` is a test module designed to verify the correctness and behavior of logging formatters used within the pytest framework, specifically focusing on colored logging output and handling of multiline log messages. It tests the integration of `ColoredLevelFormatter` and `PercentStyleMultiline` classes from the [_pytest.logging](/projects/286/67290) module, ensuring proper formatting of log records under various conditions such as color markup availability, message width/precision formatting, multiline messages, and different `auto_indent` settings.

This file contains only test functions (no classes) and primarily uses Python's built-in [logging.LogRecord](/projects/286/67290) to simulate log entries, combined with pytest's `TerminalWriter` to mimic terminal output capabilities.


Detailed Explanations

Functions


test_coloredlogformatter() -> None

**Purpose:** Tests the basic functionality of `ColoredLevelFormatter` with color markup enabled and disabled. It verifies that log records are formatted correctly with color when markup is supported and without color when markup is absent.

**Parameters:** None

**Returns:** None

**Behavior:**

**Example Usage:** This is a test function; it would be run automatically by the test framework and not called directly.


test_coloredlogformatter_with_width_precision() -> None

**Purpose:** Tests `ColoredLevelFormatter` with a log format string that includes width and precision specifiers (e.g., `%(levelname)-8.8s`). This ensures that the formatter respects these formatting constraints while applying color.

**Parameters:** None

**Returns:** None

**Behavior:**


test_multiline_message() -> None

**Purpose:** Tests the `PercentStyleMultiline` formatter's handling of multiline log messages and the effect of the `auto_indent` option on the indentation of subsequent lines.

**Parameters:** None

**Returns:** None

**Behavior:**


test_colored_short_level() -> None

**Purpose:** Tests the behavior of `ColoredLevelFormatter` when formatting a very short level name (only the first character of the level name, e.g., `%(levelname).1s`). This is important to verify that color codes are applied correctly even when the level name is truncated.

**Parameters:** None

**Returns:** None

**Behavior:**


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[Logging LogRecord] --> B[ColoredLevelFormatter]
    A --> C[PercentStyleMultiline]
    B --> D[TerminalWriter (hasmarkup)]
    C --> E[auto_indent flag]
    
    subgraph Tests
        F[test_coloredlogformatter]
        G[test_coloredlogformatter_with_width_precision]
        H[test_multiline_message]
        I[test_colored_short_level]
    end

    F --> B
    G --> B
    H --> C
    I --> B

**Diagram Explanation:**


Summary

`test_formatter.py` is a focused test suite ensuring that pytest's custom logging formatters behave correctly in formatting colored and multiline log messages. It verifies formatting output under different terminal capabilities and message structures, helping maintain robust and user-friendly logging output in pytest's test runs.