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
skipifskips on a condition, with a reason.xfailrecords a known failure without breaking the run.- A custom mark plus
-mselects a subset.
Keywords and builtins used here
__import__assertdefhasattrrangesumtest_known_wobbletest_marked_slowtest_modern_onlytest_truthy
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.
Step 1 of 3 in Selecting & faking, step 6 of 9 in Testing with pytest.