terminal.py


Overview

The [terminal.py](/projects/286/67421) file is a core module responsible for **terminal reporting of the full testing process** in the pytest framework. It manages how test execution progress, results, and summaries are presented to the user via the command-line interface (CLI). This includes:

This module acts as the **user-facing presentation layer** that translates raw test execution data into readable, clear, and concise terminal output, adapting to user-specified verbosity and output style preferences.


Detailed Explanation of Classes, Functions, and Methods

Constants and Globals


Class: MoreQuietAction(argparse.Action)

A custom argparse action to handle decreasing verbosity via `-q` or [--quiet](/projects/286/67223) command line options. Unlike the standard [count](/projects/286/67212) action (which increments), this counts down and simultaneously increments the legacy [quiet](/projects/286/67223) attribute for backwards compatibility.


NamedTuple: TestShortLogReport

Stores a concise test status representation for use during test progress display:


Function: pytest_addoption(parser: Parser) -> None

Adds command-line options and configuration ini entries related to terminal reporting and verbosity, including:

These options allow users to customize the terminal reporting experience.


Function: pytest_configure(config: Config) -> None

Registers the `TerminalReporter` plugin instance with pytest's plugin manager, enabling terminal reporting hooks and output. Also sets up tracing if debug options are enabled.


Function: getreportopt(config: Config) -> str

Parses and normalizes the `reportchars` option string, expanding aliases and ensuring warnings are included or excluded based on options.


Hook Implementation: pytest_report_teststatus(report: BaseReport) -> tuple[str, str, str]

Returns a tuple [(outcome, letter, verbose_word)](/projects/286/67223) for a given test report, used to determine the short letter and verbose status printed during test execution.


Dataclass: WarningReport

Captures information about warnings recorded during the test run.


Class: TerminalReporter

The heart of terminal reporting. Registered as a pytest plugin and responsible for all terminal output during test sessions.


Utility Functions


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

Basic Example: Running Tests With Default Terminal Reporting

pytest
# Output will show dots (.) for passed tests, F for failures, s for skips, etc.

Increasing Verbosity

pytest -v
# Shows more detailed lines about each test run including file location and reason for skips/failures.

Disabling Header and Summary

pytest --no-header --no-summary
# Suppresses the initial session header and final summary output.

Visual Diagram

classDiagram
    class TerminalReporter {
        - config: Config
        - _tw: TerminalWriter
        - stats: dict[str, list[Any]]
        - _progress_nodeids_reported: set[str]
        - _session: Session | None
        - reportchars: str
        - foldskipped: bool
        + __init__(config: Config, file: TextIO | None = None)
        + write(content: str, flush: bool = False, **markup: bool) void
        + write_line(line: str | bytes, **markup: bool) void
        + rewrite(line: str, **markup: bool) void
        + write_sep(sep: str, title: str | None, fullwidth: int | None, **markup: bool) void
        + pytest_collection() void
        + pytest_collectreport(report: CollectReport) void
        + pytest_collection_finish(session: Session) void
        + pytest_sessionstart(session: Session) void
        + pytest_runtest_logstart(nodeid: str, location: tuple[str, int | None, str]) void
        + pytest_runtest_logreport(report: TestReport) void
        + pytest_sessionfinish(session: Session, exitstatus: int | ExitCode) Generator
        + pytest_terminal_summary() Generator
        + summary_stats() void
        + short_test_summary() void
    }

    class MoreQuietAction {
        + __call__(parser: argparse.ArgumentParser, namespace: argparse.Namespace, values: str | Sequence[object] | None, option_string: str | None = None) void
    }

    MoreQuietAction ..> argparse.Action
    TerminalReporter ..> Config
    TerminalReporter ..> TerminalWriter
    TerminalReporter ..> TestReport
    TerminalReporter ..> CollectReport

Summary

The [terminal.py](/projects/286/67421) file is a comprehensive implementation of pytest's terminal reporting plugin. It provides all the logic needed to:

This module is critical for delivering a smooth and informative command-line user experience during test runs.


End of Documentation for terminal.py