Labs ICT
โญ Pro Login

pytest.mark Decorators

Using built-in markers to categorize and control tests.

1 min read | pytest
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

pytest.mark Decorators

Markers let you categorize, filter, and control test execution. pytest comes with several built-in markers, and you can create your own.

Common Built-in Markers

import pytest

@pytest.mark.slow
def test_heavy_computation():
    result = expensive_calculation()
    assert result is not None

@pytest.mark.skip(reason="Not implemented yet")
def test_future_feature():
    pass

@pytest.mark.xfail(reason="Known bug #123")
def test_known_failure():
    assert False

@pytest.mark.parametrize("input,expected", [
    (1, 2),
    (2, 4),
    (3, 6),
])
def test_double(input, expected):
    assert input * 2 == expected

Running Tests by Marker

Use the -m flag to run only tests with a specific marker:

# Run only slow tests
pytest -m slow

# Run tests that are NOT slow
pytest -m "not slow"

# Combine markers
pytest -m "slow or performance"

Registering Custom Markers

To avoid warnings, register your custom markers in pytest.ini:

[pytest]
markers =
    slow: marks tests as slow
    smoke: marks tests as quick smoke tests
    integration: marks tests requiring external services

Marker with Parameters

You can pass keyword arguments to markers:

@pytest.mark.timeout(10)
def test_quick():
    pass

@pytest.mark.skipif(
    sys.platform == "win32",
    reason="Does not work on Windows"
)
def test_unix_only():
    pass

๐Ÿงช Quick Quiz

Which decorator is used to mark a test with a label?