pytester_assertions.py


Overview

`pytester_assertions.py` is a helper plugin module designed to provide assertion utilities specifically for the `pytester` testing framework plugin within the pytest ecosystem. The module contains assertion functions that verify test run outcomes, such as the number of passed, skipped, failed, or otherwise categorized test reports.

This plugin **should not be loaded independently** because it depends on the `pytest` module's import behavior and assertion rewriting mechanism. Since `pytester` itself is imported by `pytest` and cannot be subject to assertion rewriting, these assertions are separated into this helper plugin to support `pytester`'s internal testing needs.


Detailed Description of Components

Imports


Functions

assertoutcome

def assertoutcome(
    outcomes: tuple[
        Sequence[TestReport],
        Sequence[CollectReport | TestReport],
        Sequence[CollectReport | TestReport],
    ],
    passed: int = 0,
    skipped: int = 0,
    failed: int = 0,
) -> None:

**Purpose:** Asserts that the actual test outcomes match the expected counts of passed, skipped, and failed tests.

**Parameters:**

**Returns:** None. Raises an `AssertionError` if the counts do not match.

**Usage Example:**

# Suppose 'outcomes' is obtained from a pytest test run:
outcomes = (passed_reports, skipped_reports, failed_reports)

# Assert expected counts:
assertoutcome(outcomes, passed=3, skipped=1, failed=0)

**Implementation Details:**


assert_outcomes

def assert_outcomes(
    outcomes: dict[str, int],
    passed: int = 0,
    skipped: int = 0,
    failed: int = 0,
    errors: int = 0,
    xpassed: int = 0,
    xfailed: int = 0,
    warnings: int | None = None,
    deselected: int | None = None,
) -> None:

**Purpose:** Asserts that the given dictionary of test outcome counts matches the expected count values across various categories, including passed, skipped, failed, errors, expected failures, unexpected passes, warnings, and deselected tests.

**Parameters:**

**Returns:** None. Raises an `AssertionError` if any of the expected counts do not match the actual counts.

**Usage Example:**

# outcomes dictionary obtained from a pytest test run summary:
outcomes = {
    "passed": 5,
    "failed": 1,
    "skipped": 2,
    "errors": 0,
    "xpassed": 0,
    "xfailed": 1,
    "warnings": 0,
}

# Assert expected test outcome counts:
assert_outcomes(outcomes, passed=5, failed=1, skipped=2, xfailed=1, warnings=0)

**Implementation Details:**


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[pytester_assertions.py]

    A --> B[assertoutcome]
    B --> C[outcomes: tuple of sequences]
    B --> D[expected counts: passed, skipped, failed]
    B --> E[assert actual counts == expected]

    A --> F[assert_outcomes]
    F --> G[outcomes: dict of outcome counts]
    F --> H[expected counts: passed, skipped, failed, errors, xpassed, xfailed, (optional) warnings, deselected]
    F --> I[assert actual counts == expected]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:1px
    style F fill:#bbf,stroke:#333,stroke-width:1px

Summary

`pytester_assertions.py` is a concise and specialized helper module providing assertion functions tailored for validating pytest test run outcomes in the `pytester` plugin context. It encapsulates logic to compare actual test results with expected counts across multiple categories, improving test reliability and clarity. Its careful design ensures compatibility with pytest's assertion rewriting and reporting features while segregating assertion logic from `pytester`'s core to manage import constraints.