Labs ICT
โญ Pro Login

Creating Custom Markers

Defining and registering your own markers.

Creating Custom Markers

Custom markers let you organize and filter tests based on your project's needs. You can mark tests by feature, priority, environment, or any category you want.

Defining Custom Markers

import pytest

# Register markers in pytest.ini
# [pytest]
# markers =
#     smoke: quick smoke tests
#     regression: full regression suite
#     api: tests for API endpoints

@pytest.mark.smoke
def test_homepage_loads():
    assert get_homepage_status() == 200

@pytest.mark.regression
def test_all_user_flows():
    complete_user_journey()

@pytest.mark.api
def test_login_endpoint():
    response = post("/api/login", data={})
    assert response.status_code == 200

Markers with Parameters

Pass extra information to markers using keyword arguments:

@pytest.mark.regression(version="2.1")
def test_feature_v2():
    pass

@pytest.mark.api(method="POST", endpoint="/users")
def test_create_user():
    pass

# Access marker data in hooks
def pytest_collection_modifyitems(items):
    for item in items:
        for marker in item.iter_markers("api"):
            print(marker.kwargs)

Selecting by Custom Markers

Run tests filtered by your custom markers:

# Run only smoke tests
pytest -m smoke

# Run only API tests
pytest -m api

# Combine markers
pytest -m "smoke and api"

# Exclude slow tests
pytest -m "not regression"

Marker Registration

Always register custom markers to avoid warnings. In pytest.ini:

[pytest]
markers =
    smoke: Quick smoke tests for critical paths
    regression: Full regression test suite
    api: Tests for API endpoints
    slow: Tests that take more than 10 seconds

๐Ÿงช Quick Quiz

Where do you register custom markers in pytest?