SyntaxStudy
Sign Up
Python Test Marks and Categories
Python Intermediate 3 min read

Test Marks and Categories

pytest Marks

Marks categorise tests for selective runs, skipping, and expected failures.

Example
import pytest

@pytest.mark.slow
def test_heavy_computation(): ...

@pytest.mark.skip(reason="feature not yet implemented")
def test_future_feature(): ...

@pytest.mark.skipif(sys.platform == "win32", reason="POSIX only")
def test_unix_paths(): ...

@pytest.mark.xfail(reason="known bug #123")
def test_known_issue(): ...

# Run only slow tests
# pytest -m slow
# Exclude slow tests
# pytest -m "not slow"
Pro Tip

Register custom marks in pytest.ini to avoid PytestUnknownMarkWarning.