test_skipping.py


Overview

The `test_skipping.py` file is a comprehensive test suite designed to validate the behavior of pytest's skipping and expected failure (xfail) mechanisms. It focuses on verifying the correct evaluation and handling of `skip`, `skipif`, and `xfail` markers and functions within the pytest testing framework.

The file contains multiple test classes and standalone test functions that:

This file heavily leverages pytest's own testing utilities (e.g., [Pytester](/projects/286/67470) fixture) to create inline test files, run pytest sessions, and assert expected outcomes.


Classes and Functions

Class: TestEvaluation

**Purpose:** Tests the evaluation logic of `skip` and `xfail` markers applied to test items. It verifies how conditions, reasons, and multiple markers are processed and how errors in marker usage are handled.

**Key Methods:**


Class: TestXFail

**Purpose:** Focuses on testing the behavior of the `xfail` marker, including strictness, conditional evaluation, reporting formats, and dynamic application of xfail markers.

**Key Methods:**


Class: TestXFailwithSetupTeardown

**Purpose:** Tests xfail behavior when setup or teardown functions raise exceptions.

**Key Methods:**


Class: TestSkip

**Purpose:** Tests various scenarios of the `skip` marker and [pytest.skip()](/projects/286/67266) function, including class-level skips, skips with reasons, and error cases.

**Key Methods:**


Class: TestSkipif

**Purpose:** Tests the `skipif` marker in various forms, including conditional skipping, reporting, and interaction with platform and multiple markers.

**Key Methods:**


Standalone Test Functions

The file also contains numerous standalone test functions testing:


Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

Since this is a test suite, usage is primarily for pytest developers or contributors to run and verify pytest's skipping and xfail functionality.

Example to run the tests in this file:

pytest test_skipping.py

Example of a snippet tested inside:

import pytest

@pytest.mark.skipif("hasattr(os, 'sep')", reason="OS does not have sep")
def test_func():
    pass

This should cause the test to be skipped with the reason `"OS does not have sep"`.


Mermaid Class Diagram of test_skipping.py

classDiagram
    class TestEvaluation {
        +test_no_marker(pytester)
        +test_marked_xfail_no_args(pytester)
        +test_marked_skipif_no_args(pytester)
        +test_marked_one_arg(pytester)
        +test_marked_one_arg_with_reason(pytester)
        +test_marked_one_arg_twice(pytester)
        +test_marked_one_arg_twice2(pytester)
        +test_marked_skipif_with_boolean_without_reason(pytester)
        +test_marked_skipif_with_invalid_boolean(pytester)
        +test_skipif_class(pytester)
        +test_skipif_markeval_namespace(pytester)
        +test_skipif_markeval_namespace_multiple(pytester)
        +test_skipif_markeval_namespace_ValueError(pytester)
    }

    class TestXFail {
        +test_xfail_simple(pytester, strict)
        +test_xfail_xpassed(pytester)
        +test_xfail_using_platform(pytester)
        +test_xfail_xpassed_strict(pytester)
        +test_xfail_run_anyway(pytester)
        +test_xfail_run_with_skip_mark(pytester, test_input, expected)
        +test_xfail_evalfalse_but_fails(pytester)
        +test_xfail_not_run_xfail_reporting(pytester)
        +test_xfail_not_run_no_setup_run(pytester)
        +test_xfail_xpass(pytester)
        +test_xfail_imperative(pytester)
        +test_xfail_imperative_in_setup_function(pytester)
        +test_dynamic_xfail_set_during_funcarg_setup(pytester)
        +test_dynamic_xfail_set_during_runtest_failed(pytester)
        +test_dynamic_xfail_set_during_runtest_passed_strict(pytester)
        +test_xfail_raises(expected, actual, matchline, pytester)
        +test_strict_sanity(pytester)
        +test_strict_xfail(pytester, strict)
        +test_strict_xfail_condition(pytester, strict)
        +test_xfail_condition_keyword(pytester, strict)
        +test_strict_xfail_default_from_file(pytester, strict_val)
        +test_xfail_markeval_namespace(pytester)
    }

    class TestXFailwithSetupTeardown {
        +test_failing_setup_issue9(pytester)
        +test_failing_teardown_issue9(pytester)
    }

    class TestSkip {
        +test_skip_class(pytester)
        +test_skips_on_false_string(pytester)
        +test_arg_as_reason(pytester)
        +test_skip_no_reason(pytester)
        +test_skip_with_reason(pytester)
        +test_only_skips_marked_test(pytester)
        +test_strict_and_skip(pytester)
        +test_wrong_skip_usage(pytester)
    }

    class TestSkipif {
        +test_skipif_conditional(pytester)
        +test_skipif_reporting(pytester, params)
        +test_skipif_using_platform(pytester)
        +test_skipif_reporting_multiple(pytester, marker, msg1, msg2)
    }

    TestEvaluation <|-- TestXFail
    TestEvaluation <|-- TestXFailwithSetupTeardown
    TestEvaluation <|-- TestSkip
    TestEvaluation <|-- TestSkipif

Summary

`test_skipping.py` is a detailed pytest test suite ensuring the correctness and robustness of pytest's skip and xfail features. It covers a wide range of cases, from simple marker usage to complex dynamic and conditional skipping/failing, including error handling and reporting accuracy. This file is crucial for maintaining pytest's core functionality related to test skipping and expected failures.


If you need further explanation on specific tests or integration points, feel free to ask!