skipping.py


Overview

The [skipping.py](/projects/286/67623) module is a core part of pytest's test execution framework that provides support for **skipping tests** and marking **expected failures (xfail)**. It enables conditional test skipping and expected failure handling based on dynamically evaluated conditions or static markers. This allows test authors to declaratively specify when a test should be skipped or considered an expected failure, improving test suite flexibility, reporting clarity, and robustness.

Key capabilities include:


Classes, Functions, and Methods

1. pytest_addoption(parser: Parser) -> None

**Purpose:** Adds command-line options and ini-file configuration related to skipping and xfail functionality.

**Details:**

**Usage Example:**

pytest --runxfail

or in `pytest.ini`:

[pytest]
xfail_strict = true

2. pytest_configure(config: Config) -> None

**Purpose:** Performs initial configuration hooks for skip and xfail support.

**Details:**


3. evaluate_condition(item: Item, mark: Mark, condition: object) -> tuple[bool, str]

**Purpose:** Evaluates a single condition from a `skipif` or `xfail` marker to determine if it triggers skipping/xfail.

**Parameters:**

**Returns:** A tuple `(result: bool, reason: str)` where `result` indicates if the condition is met, and `reason` provides the reason for skip/xfail.

**Implementation Details:**

**Example Usage:**

result, reason = evaluate_condition(item, mark, "sys.platform == 'win32'")
if result:
    # condition triggered

4. @dataclasses.dataclass(frozen=True) class Skip

**Purpose:** Represents the result of evaluating skip markers on a test item.

**Attributes:**


5. evaluate_skip_marks(item: Item) -> Skip | None

**Purpose:** Evaluates all `skip` and `skipif` markers on the given test item to decide if the test should be skipped.

**Returns:**

**Implementation Details:**


6. @dataclasses.dataclass(frozen=True) class Xfail

**Purpose:** Represents the result of evaluating xfail markers on a test item.

**Attributes:**


7. evaluate_xfail_marks(item: Item) -> Xfail | None

**Purpose:** Evaluates all `xfail` markers on the test item and returns an `Xfail` instance if any conditions are met.

**Returns:**

**Implementation Details:**


8. xfailed_key = StashKey[Optional[Xfail]]()

**Purpose:** A unique key used to stash the result of xfail evaluation inside a test item's stash dictionary, enabling efficient reuse during test phases.


9. pytest_runtest_setup(item: Item) -> None

**Purpose:** Pytest hook called before running a test, responsible for applying skip and xfail logic.

**Behavior:**


10. pytest_runtest_call(item: Item) -> Generator[None]

**Purpose:** Wraps the test call phase, managing xfail behavior.

**Behavior:**


11. pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> Generator[None, TestReport, TestReport]

**Purpose:** Wraps the test reporting phase to modify reports based on skip and xfail status.

**Behavior:**


12. pytest_report_teststatus(report: BaseReport) -> tuple[str, str, str] | None

**Purpose:** Customizes terminal test status reporting for xfail/skipped tests.

**Returns:**


Important Implementation Details and Algorithms


Interaction with Other System Components

The module hooks into pytest’s lifecycle via:


Mermaid Class Diagram

classDiagram
    class Skip {
        +reason: str
    }
    class Xfail {
        +reason: str
        +run: bool
        +strict: bool
        +raises: type or tuple or AbstractRaises or None
    }
    class StashKey~T~ {
        <<generic>>
    }
    class Mark {
        +name: str
        +args: tuple
        +kwargs: dict
    }
    class Item {
        +config: Config
        +stash: dict
        +iter_markers(name: str) Mark[]
    }
    class CallInfo~T~ {
        +excinfo: ExceptionInfo | None
        +when: str
    }
    class TestReport {
        +wasxfail: Optional[str]
        +outcome: str
        +longrepr: Optional[str]
    }

    Skip ..> Mark : uses
    Xfail ..> Mark : uses
    Item ..> Mark : has markers
    Item ..> StashKey~Xfail~ : uses for xfail cache
    CallInfo ..> Exception : may contain
    pytest_runtest_makereport ..> TestReport : modifies

Usage Summary

# Example: Mark a test to skip on Windows platforms
@pytest.mark.skipif(sys.platform == "win32", reason="Does not run on Windows")
def test_windows_specific():
    ...

# Example: Mark a test as expected to fail if an environment variable is set
@pytest.mark.xfail(os.getenv("BUGGY_FEATURE") == "1", reason="Known bug #123", strict=True)
def test_buggy_feature():
    ...

# During test setup, skipping and xfail markers are evaluated automatically
# by pytest via the hooks defined in this module.

Summary

The [skipping.py](/projects/286/67623) module is essential for pytest’s flexible test skipping and expected failure management. It ensures that tests can be conditionally excluded or expected to fail without disrupting test suite execution and reporting. By providing a robust evaluation mechanism, integration with pytest’s lifecycle, and clear feedback to users, it significantly enhances test suite maintainability and clarity.


End of skipping.py documentation.