outcomes.py

Overview

The [outcomes.py](/projects/286/67331) module is a core utility file designed to handle test outcomes within a testing framework, specifically pytest. It defines exception classes that represent various test results such as skips, failures, and expected failures (xfail). Additionally, it provides imperative functions (`skip()`, `fail()`, `xfail()`, `exit()`) that raise these exceptions to communicate specific test outcomes programmatically during test execution or collection.

A key feature of this module is the `importorskip()` function, which attempts to import a module and skips the current test if the import fails or if the module version is insufficient. This utility aids in writing conditional tests that depend on optional dependencies.

Overall, [outcomes.py](/projects/286/67331) provides a structured, exception-based mechanism for signaling test outcomes and controlling test flow by raising specialized exceptions that the pytest framework interprets to generate reports, skip tests, or stop execution.


Classes

OutcomeException(BaseException)

Base class for exceptions that represent test and collection outcomes. Subclasses of `OutcomeException` encapsulate information about test results like skipping or failing.


Skipped(OutcomeException)

Exception indicating a test was skipped.


Failed(OutcomeException)

Exception indicating a test failure triggered explicitly (e.g., via `pytest.fail()`).


Exit(Exception)

Exception used for immediate program exits without tracebacks or summaries.


XFailed(Failed)

Exception indicating an expected failure (`pytest.xfail()`).


_Exit

Callable class to raise an `Exit` exception.


_Skip

Callable class to raise a `Skipped` exception.


_Fail

Callable class to raise a `Failed` exception.


_XFail

Callable class to raise an `XFailed` exception.


Functions

importorskip(modname: str, minversion: str | None = None, reason: str | None = None, *, exc_type: type[ImportError] | None = None) -> Any

Attempts to import a module by name. If the module cannot be imported, or if the module's version is less than the specified minimum version, it raises a `Skipped` exception to skip the current test.


Module-level Variables (Callable Instances)

These variables are instances of the callable classes above, providing a simple API to raise outcome exceptions:


Important Implementation Details


Interactions with Other Parts of the System


Visual Diagram

classDiagram
    class OutcomeException {
        +msg: str | None
        +pytrace: bool
        +__init__(msg: str | None, pytrace: bool)
        +__repr__() str
    }
    class Skipped {
        +allow_module_level: bool
        +_use_item_location: bool
        +__init__(msg: str | None, pytrace: bool, allow_module_level: bool, _use_item_location: bool)
    }
    class Failed {
        +__init__(msg: str | None, pytrace: bool)
    }
    class Exit {
        +msg: str
        +returncode: int | None
        +__init__(msg: str, returncode: int | None)
    }
    class XFailed {
        +__init__(msg: str | None, pytrace: bool)
    }
    class _Exit {
        +Exception: Exit
        +__call__(reason: str, returncode: int | None) -> NoReturn
    }
    class _Skip {
        +Exception: Skipped
        +__call__(reason: str, allow_module_level: bool) -> NoReturn
    }
    class _Fail {
        +Exception: Failed
        +__call__(reason: str, pytrace: bool) -> NoReturn
    }
    class _XFail {
        +Exception: XFailed
        +__call__(reason: str) -> NoReturn
    }
    OutcomeException <|-- Skipped
    OutcomeException <|-- Failed
    Failed <|-- XFailed
    Exception <|-- Exit
    _Exit --> Exit : Exception
    _Skip --> Skipped : Exception
    _Fail --> Failed : Exception
    _XFail --> XFailed : Exception

Summary

The [outcomes.py](/projects/286/67331) module provides a clean and extensible exception-based mechanism for signaling test outcomes within pytest. By defining specialized exceptions and callable wrappers, it simplifies the process of skipping, failing, marking expected failures, and exiting tests programmatically. The `importorskip()` utility further enhances test flexibility by enabling conditional imports with graceful skipping and version validation.

This module underpins key control flow decisions during test runs, contributing to pytest's robust and user-friendly testing experience.