reports.py


Overview

The `reports.py` file defines core data structures and serialization logic for test reports within the testing framework. It encapsulates detailed information about individual test outcomes and collection results, including their status, timing, captured output, and exception information.

This file provides:

These reports serve as the fundamental data units communicated between test execution, reporting subsystems (e.g., terminal output, JUnit XML generation), and plugins.


Classes and Functions

getworkerinfoline(node)

Fetches and caches a formatted string representing worker information (usually for distributed test execution contexts) attached to a pytest node.

**Usage example:**

worker_line = getworkerinfoline(test_node)
print(worker_line)

class BaseReport

Base class representing the outcome of a test phase or collection step. This class is extended by specialized reports.

**Attributes:**

**Key Methods and Properties:**


class TestReport(BaseReport)

Specialized report representing the result of running a test phase (`setup`, `call`, or `teardown`).

**Additional Attributes:**

**Constructor Parameters:**

def __init__(
    self,
    nodeid: str,
    location: tuple[str, int | None, str],
    keywords: Mapping[str, Any],
    outcome: Literal["passed", "failed", "skipped"],
    longrepr: None | ExceptionInfo[BaseException] | tuple[str, int, str] | str | TerminalRepr,
    when: Literal["setup", "call", "teardown"],
    sections: Iterable[tuple[str, str]] = (),
    duration: float = 0,
    start: float = 0,
    stop: float = 0,
    user_properties: Iterable[tuple[str, object]] | None = None,
    **extra,
) -> None

**Notable Methods:**

**Usage example:**

report = TestReport.from_item_and_call(test_item, callinfo)
print(report.outcome)
print(report.duration)

class CollectReport(BaseReport)

Report representing the outcome of a test collection phase.

**Attributes:**

**Constructor Parameters:**

def __init__(
    self,
    nodeid: str,
    outcome: Literal["passed", "failed", "skipped"],
    longrepr: None | ExceptionInfo[BaseException] | tuple[str, int, str] | str | TerminalRepr,
    result: list[Item | Collector] | None,
    sections: Iterable[tuple[str, str]] = (),
    **extra,
) -> None

**Usage example:**

collect_report = CollectReport(nodeid="tests/test_module.py", outcome="passed", longrepr=None, result=collected_items)

class CollectErrorRepr(TerminalRepr)

Simple terminal representation of a collection error message.


Serialization Functions

These functions support converting reports to/from JSON-serializable dictionaries for inter-process communication or persistence.


Important Implementation Details


Interaction With Other System Components


Visual Diagram: Class Structure of reports.py

classDiagram
    class BaseReport {
        +str when
        +tuple location
        +longrepr
        +list~tuple~sections~
        +str nodeid
        +str outcome
        +__init__(**kw)
        +toterminal(out: TerminalWriter)
        +get_sections(prefix: str)
        +longreprtext
        +caplog
        +capstdout
        +capstderr
        +passed
        +failed
        +skipped
        +fspath
        +count_towards_summary
        +head_line
        +_get_verbose_word_with_markup(config, default_markup)
        +_to_json() dict
        +_from_json(reportdict) Self
    }

    class TestReport {
        +str nodeid
        +tuple location
        +Mapping keywords
        +str outcome
        +longrepr
        +str when
        +list sections
        +float duration
        +float start
        +float stop
        +list user_properties
        +wasxfail
        +__init__(...)
        +from_item_and_call(item, call) TestReport
        +__repr__()
    }

    class CollectReport {
        +str nodeid
        +str outcome
        +longrepr
        +list result
        +list sections
        +when = "collect"
        +__init__(...)
        +location property
        +__repr__()
    }

    class CollectErrorRepr {
        +str longrepr
        +__init__(msg)
        +toterminal(out: TerminalWriter)
    }

    BaseReport <|-- TestReport
    BaseReport <|-- CollectReport
    TerminalRepr <|-- CollectErrorRepr

Summary

`reports.py` is a foundational module defining how test results and collection data are represented, serialized, and displayed within the testing framework. The `BaseReport` class encapsulates common report behavior, while `TestReport` and `CollectReport` specialize for test execution phases and collection respectively. Serialization helpers enable inter-process communication, crucial for distributed test runs.

This module closely interacts with the test execution engine, terminal reporters, and plugins, serving as the canonical source of truth for test outcomes and their rich contextual data.


End of reports.py Documentation