truncate.py


Overview

The `truncate.py` file provides utility functions to manage the truncation of assertion output explanations in test runs, specifically within the Pytest framework. Its main purpose is to control the verbosity of assertion failure explanations by truncating overly long outputs based on configurable limits on lines and characters. This helps keep test output concise and readable during normal test runs, while still allowing full verbosity when desired (e.g., when running tests with higher verbosity flags or in continuous integration environments).

The truncation logic is sensitive to factors such as verbosity level, environment (CI or local), and user-defined configuration parameters. When truncation occurs, a clear usage message guides the user on how to view the full output.


Detailed Description of Functions

truncate_if_required(explanation: list[str], item: Item) -> list[str]

**Purpose:** Truncates an assertion explanation if the given test item meets criteria for truncation, otherwise returns the original explanation unchanged.

**Parameters:**

**Returns:**

**Usage Example:**

explanation = [
    "Assertion failed because x != y",
    "x = 5",
    "y = 10",
    "...",
]
truncated = truncate_if_required(explanation, test_item)
for line in truncated:
    print(line)

_get_truncation_parameters(item: Item) -> tuple[bool, int, int]

**Purpose:** Determines whether truncation is needed for the given test item, and retrieves the truncation limits for lines and characters.

**Parameters:**

**Returns:**

**Important Details:**


_truncate_explanation(input_lines: list[str], max_lines: int, max_chars: int) -> list[str]

**Purpose:** Truncates a list of strings representing an assertion explanation to comply with maximum line and character limits. Adds a usage message indicating how to see full output.

**Parameters:**

**Returns:**

**Algorithm / Implementation Details:**


_truncate_by_char_count(input_lines: list[str], max_chars: int) -> list[str]

**Purpose:** Helper function to truncate a list of strings to ensure the total character count does not exceed a specified maximum, truncating the last line if necessary.

**Parameters:**

**Returns:**

**Implementation Details:**


Constants


Interaction With Other Parts of the System

The file is typically invoked during assertion introspection phases in pytest, controlling how assertion failure explanations are displayed based on user preferences and environment.


Visual Diagram

flowchart TD
    A[truncate_if_required] --> B[_get_truncation_parameters]
    A --> C[_truncate_explanation]
    C --> D[_truncate_by_char_count]

    subgraph Explanation Flow
        B -->|returns truncation flags| A
        A -->|if truncate| C
        C -->|if needed| D
    end

**Diagram Explanation:**


Summary

The `truncate.py` module in pytest is a utility focused on managing the verbosity of assertion failure outputs by truncating them based on configurable line and character limits. It smartly decides when truncation should occur based on verbosity settings and environment (e.g., CI). The truncation process carefully preserves output readability and provides clear messages to users on how to see full assertion details. This enhances the usability of test output during development and automated runs.


Example Usage in Pytest Context

def test_example(item):
    explanation = [
        "Assertion failed:",
        "x = 1234567890",
        "y = 0987654321",
        # potentially many more lines...
    ]
    truncated_output = truncate_if_required(explanation, item)
    for line in truncated_output:
        print(line)

This will print a truncated explanation based on the current test item's verbosity and configuration.


End of Documentation for truncate.py