skipping.rst

Overview

This documentation file provides an extensive tutorial and reference guide on how to use **skip** and **xfail** markers and functions within the pytest testing framework. Its primary purpose is to instruct users on managing test cases that are either not expected to run successfully or should be conditionally ignored, enabling a clean and meaningful test suite report.

The file explains the rationale behind skipping tests (tests that are not applicable or cannot run under certain conditions) and expected failures (tests that are known to fail due to bugs, unimplemented features, or environmental issues). It covers usage patterns, parameter options, and best practices for marking tests to control test execution flow and improve test suite maintainability.

Contents include:

The document also explains how these markers affect pytest's test collection, execution, and reporting behaviors.


Detailed Explanations

1. Skipping Tests

Skipping tests means instructing pytest not to run certain tests under specific conditions. This can be useful for platform-dependent tests, missing resource conditions, or temporarily disabling unstable tests.

1.1 @pytest.mark.skip

Decorator to unconditionally skip a test function or method.

**Parameters:**

**Example:**

@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    ...

**Usage Notes:**

1.2 pytest.skip(reason)

Imperative function to skip tests during runtime, useful when skip condition can't be evaluated at import time.

**Parameters:**

**Example:**

def test_function():
    if not valid_config():
        pytest.skip("unsupported configuration")

**Additional:**

Skipping whole modules by calling `pytest.skip(reason, allow_module_level=True)` at module level is supported.

1.3 @pytest.mark.skipif(condition, reason)

Decorator for conditional skipping — skips the test if `condition` evaluates to `True`.

**Parameters:**

**Example:**

import sys

@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
def test_function():
    ...

**Usage Notes:**

1.4 Skipping based on missing imports

Use `pytest.importorskip("module_name")` to skip tests if an import fails or version requirements are not met.

**Example:**

docutils = pytest.importorskip("docutils", minversion="0.3")

This skips tests if `docutils` is not installed or version < 0.3.


2. Expected Failures (xfail)

Mark tests that are expected to fail — useful for known bugs, unimplemented features, or flaky tests.

2.1 @pytest.mark.xfail

Decorator marking tests expected to fail.

**Parameters:**

**Example:**

@pytest.mark.xfail(reason="known parser issue")
def test_function():
    ...
@pytest.mark.xfail(sys.platform == "win32", reason="bug in a 3rd party library")
def test_function():
    ...
@pytest.mark.xfail(raises=RuntimeError)
def test_function():
    ...
@pytest.mark.xfail(run=False)
def test_function():
    ...
@pytest.mark.xfail(strict=True)
def test_function():
    ...

2.2 pytest.xfail(reason)

Imperative call to mark a test as expected to fail within the test or setup function.

**Example:**

def test_function():
    if not valid_config():
        pytest.xfail("failing configuration (but should work)")

Note: When called, no further test code is executed as `pytest.xfail` raises a special exception internally.

2.3 Command line options


3. Skip/Xfail with Parameterized Tests

Markers can be applied to individual test instances when using `pytest.mark.parametrize` by marking parameters explicitly.

**Example:**

import sys
import pytest

@pytest.mark.parametrize(
    ("n", "expected"),
    [
        (1, 2),
        pytest.param(1, 0, marks=pytest.mark.xfail),
        pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
        (2, 3),
        (3, 4),
        (4, 5),
        pytest.param(
            10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k")
        ),
    ],
)
def test_increment(n, expected):
    assert n + 1 == expected

Implementation Details & Algorithms


Interactions with Other Parts of the System


Visual Diagram

The following Mermaid class diagram represents the core concepts and relationships of skip and xfail markers and functions as used in this file:

classDiagram
    class SkipMarker {
        +reason: str
        +condition: bool (optional)
        +__call__()
    }
    class XFailMarker {
        +condition: bool (optional)
        +reason: str (optional)
        +raises: Exception or tuple (optional)
        +run: bool = True
        +strict: bool = False
        +__call__()
    }
    class PytestFunctions {
        +skip(reason: str, allow_module_level: bool = False)
        +xfail(reason: str)
        +importorskip(module_name: str, minversion: str = None)
    }
    class TestFunction {
        +name: str
        +markers: list
        +run()
    }

    SkipMarker <|-- TestFunction : applied to
    XFailMarker <|-- TestFunction : applied to
    PytestFunctions ..> TestFunction : runtime control

Summary

This file serves as a comprehensive guide to using pytest's skip and xfail features, providing both declarative (decorator) and imperative (function call) methods to control test execution based on dynamic conditions or known test statuses. It helps maintain a clean test report by clearly signaling tests that are not run or are expected to fail, improving test suite reliability and developer productivity.