typestar

Marks and skipping in Python

Marks label tests so a run can select or expect them.

import sys

import pytest


@pytest.mark.skipif(sys.version_info < (3, 11), reason="needs TaskGroup")
def test_modern_only():
    assert hasattr(__import__("asyncio"), "TaskGroup")


@pytest.mark.xfail(reason="rounding differs across platforms", strict=False)
def test_known_wobble():
    assert 0.1 + 0.2 == 0.3


@pytest.mark.slow
def test_marked_slow():
    assert sum(range(1000)) == 499500


@pytest.mark.parametrize("value", [1, pytest.param(0, marks=pytest.mark.xfail)])
def test_truthy(value):
    assert value

How it works

  1. skipif skips on a condition, with a reason.
  2. xfail records a known failure without breaking the run.
  3. A custom mark plus -m selects a subset.

Keywords and builtins used here

The run, in numbers

Lines
23
Characters to type
502
Tokens
129
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 70 seconds.

Type this snippet

Step 1 of 3 in Selecting & faking, step 6 of 9 in Testing with pytest.

← Previous Next →