Skip Marker Evaluation

Purpose

Skip Marker Evaluation addresses the need to conditionally or unconditionally skip tests before they are executed. Within the broader topic of test skipping and expected failure handling, this subtopic focuses specifically on evaluating `skip` and `skipif` markers applied to test items, determining when a test should be bypassed based on user-defined conditions or static decisions. This evaluation helps avoid unnecessary test runs (e.g., on unsupported platforms or unmet dependencies), improving test suite efficiency and clarity.

Functionality

The core of Skip Marker Evaluation lies in analyzing the skip-related markers attached to a test item and deciding whether the test should be skipped, and if so, providing an appropriate reason.

Key steps and workflows include:

Example snippet illustrating condition evaluation logic:

def evaluate_condition(item: Item, mark: Mark, condition: object) -> tuple[bool, str]:
    if isinstance(condition, str):
        # Evaluate string condition with safe globals
        result = eval(condition_code, globals_)
    else:
        # Evaluate boolean condition
        result = bool(condition)
    reason = mark.kwargs.get("reason") or f"condition: {condition}" if isinstance(condition, str) else fail(...)
    return result, reason

Integration

Skip Marker Evaluation integrates tightly with the parent topic by serving as the gatekeeper step that determines whether a test should be executed or skipped. It complements the handling of expected failures (`xfail`) by ensuring that tests flagged as skipped are not run unnecessarily.

Diagram

A flowchart best illustrates the decision process for evaluating skip markers on a test item:

flowchart TD
    Start[Start Skip Evaluation]
    GetMarkers[Retrieve skipif & skip markers]
    CheckSkipif{Any skipif markers?}
    EvalConditions[Evaluate each skipif condition]
    ConditionTrue{Condition True?}
    ReturnSkipif[Return Skip(reason)]
    CheckSkip{Any skip markers?}
    ReturnSkip[Return Skip(reason)]
    NoSkip[Return None - no skip]
    
    Start --> GetMarkers
    GetMarkers --> CheckSkipif
    CheckSkipif -- Yes --> EvalConditions
    EvalConditions --> ConditionTrue
    ConditionTrue -- Yes --> ReturnSkipif
    ConditionTrue -- No --> CheckSkip
    CheckSkipif -- No --> CheckSkip
    CheckSkip -- Yes --> ReturnSkip
    CheckSkip -- No --> NoSkip

This process ensures that tests are skipped precisely according to user-defined conditions or unconditional skip declarations, optimizing test runs and improving clarity in test outcomes.