test_reports.py


Overview

The [test_reports.py](/projects/286/67344) file contains a comprehensive suite of automated tests that verify the serialization, deserialization, and functional correctness of pytest's internal reporting objects, primarily focusing on `TestReport` and `CollectReport` classes. These reports encapsulate information about test execution (e.g., outcomes, tracebacks, timing).

The tests ensure that:

This file is essential for maintaining the integrity of pytest's distributed and remote testing capabilities (such as `xdist`), where test reports need to be transferred between processes or machines.


Classes and Their Methods

Class: TestReportSerialization

This class contains multiple test methods validating the behavior of `TestReport` serialization and deserialization under various scenarios. It uses the `pytester` fixture extensively to create and run test cases within a temporary pytest environment.

Method Name

Purpose

Parameters

Returns

Usage Example

`test_xdist_longrepr_to_str_issue_241`

Verifies serialization of long traceback representations including `reprtraceback` style.

`pytester: Pytester`

None

See below

`test_xdist_report_longrepr_reprcrash_130`

Tests serialization of reports with a `longrepr` containing an `ExceptionRepr` with sections.

`pytester: Pytester`

None

See below

`test_reprentries_serialization_170`

Checks serialization of detailed traceback entries (`ReprEntry`) including locals and args.

`pytester: Pytester`

None

See below

`test_reprentries_serialization_196`

Similar to above but for `ReprEntryNative` style traceback entries.

`pytester: Pytester`

None

See below

`test_itemreport_outcomes`

Validates correctness of outcome flags (`passed`, `failed`, `skipped`, `xfail`) after serialization.

`pytester: Pytester`

None

See below

`test_collectreport_passed`

Tests serialization of `CollectReport` objects when collection passes.

`pytester: Pytester`

None

See below

`test_collectreport_fail`

Tests serialization of `CollectReport` objects when collection fails (syntax errors, etc.).

`pytester: Pytester`

None

See below

`test_extended_report_deserialization`

Ensures additional attributes are preserved during serialization of `CollectReport` objects.

`pytester: Pytester`

None

See below

`test_paths_support`

Ensures that path-like objects in reports serialize to string paths correctly.

`pytester: Pytester`

None

See below

`test_deserialization_failure`

Tests error handling when deserializing unknown traceback entry types.

`pytester: Pytester`

None

See below

`test_chained_exceptions`

Verifies correct serialization of reports with chained exceptions (`ExceptionChainRepr`).

`pytester: Pytester`, `tw_mock`, `report_class` (parametrized)

None

See below

`test_chained_exceptions_no_reprcrash`

Regression test for chained exceptions lacking a `reprcrash` (relevant for remote exceptions).

`pytester: Pytester`, `tw_mock`

None

See below

`test_report_prevent_ConftestImportFailure_hiding_exception`

Ensures import errors in conftest files are not masked during reporting.

`pytester: Pytester`

None

See below

`test_report_timestamps_match_duration`

Checks that report timestamps align with reported test durations.

`pytester: Pytester`, `mock_timing`

None

See below

**Usage Example:**

def test_xdist_longrepr_to_str_issue_241(self, pytester: Pytester) -> None:
    pytester.makepyfile(
        '''
        def test_a(): assert False
        def test_b(): pass
        '''
    )
    reprec = pytester.inline_run()
    reports = reprec.getreports("pytest_runtest_logreport")
    test_a_call = reports[1]
    assert test_a_call.when == "call"
    assert test_a_call.outcome == "failed"
    data = test_a_call._to_json()
    assert data["longrepr"]["reprtraceback"]["style"] == "long"

Class: TestHooks

This class tests the integration points (hooks) pytest provides for report serialization and deserialization, ensuring that plugins or other pytest extensions can customize or extend report behavior correctly.

Method Name

Purpose

Parameters

Returns

Usage Example

`test_test_report`

Tests the hooks for serializing and deserializing `TestReport` objects.

`pytester: Pytester, pytestconfig: Config`

None

See below

`test_collect_report`

Tests the hooks for serializing and deserializing `CollectReport` objects.

`pytester: Pytester, pytestconfig: Config`

None

See below

`test_invalid_report_types`

Ensures that unknown report types cause assertion errors during deserialization via hooks.

`pytester: Pytester, pytestconfig: Config, hook_name: str`

None

See below

**Usage Example:**

def test_test_report(self, pytester: Pytester, pytestconfig: Config) -> None:
    pytester.makepyfile(
        '''
        def test_a(): assert False
        def test_b(): pass
        '''
    )
    reprec = pytester.inline_run()
    reports = reprec.getreports("pytest_runtest_logreport")
    for rep in reports:
        data = pytestconfig.hook.pytest_report_to_serializable(config=pytestconfig, report=rep)
        assert data["$report_type"] == "TestReport"
        new_rep = pytestconfig.hook.pytest_report_from_serializable(config=pytestconfig, data=data)
        assert new_rep.nodeid == rep.nodeid

Important Implementation Details


Interaction with Other Parts of the Application


Visual Diagram: Class Diagram for TestReportSerialization and TestHooks

classDiagram
    class TestReportSerialization {
        +test_xdist_longrepr_to_str_issue_241(pytester)
        +test_xdist_report_longrepr_reprcrash_130(pytester)
        +test_reprentries_serialization_170(pytester)
        +test_reprentries_serialization_196(pytester)
        +test_itemreport_outcomes(pytester)
        +test_collectreport_passed(pytester)
        +test_collectreport_fail(pytester)
        +test_extended_report_deserialization(pytester)
        +test_paths_support(pytester)
        +test_deserialization_failure(pytester)
        +test_chained_exceptions(pytester, tw_mock, report_class)
        +test_chained_exceptions_no_reprcrash(pytester, tw_mock)
        +test_report_prevent_ConftestImportFailure_hiding_exception(pytester)
        +test_report_timestamps_match_duration(pytester, mock_timing)
    }

    class TestHooks {
        +test_test_report(pytester, pytestconfig)
        +test_collect_report(pytester, pytestconfig)
        +test_invalid_report_types(pytester, pytestconfig, hook_name)
    }

Summary

[test_reports.py](/projects/286/67344) is a critical test module that ensures pytest's test reporting infrastructure, especially serialization and deserialization of reports, is robust, accurate, and extensible. It covers a wide range of scenarios from basic pass/fail/skipped outcomes to complex chained exceptions and plugin hook integrations. This thorough coverage supports pytest's distributed testing capabilities and plugin ecosystem.


End of Documentation for test_reports.py