0
0
PyTesttesting~5 mins

Marker registration in PyTest

Choose your learning style9 modes available
Introduction

Markers help you label tests so you can run or skip groups easily.

You want to run only slow tests separately from fast ones.
You want to skip tests that need a database when it's not available.
You want to mark tests that check a new feature under development.
You want to group tests by priority or type for better organization.
Syntax
PyTest
[pytest]
markers =
    marker_name: description of the marker

# In test file
import pytest

@pytest.mark.marker_name
def test_example():
    assert True

Markers must be registered in pytest.ini to avoid warnings.

Use @pytest.mark.marker_name above test functions to apply markers.

Examples
This example registers a slow marker and uses it on a test.
PyTest
[pytest]
markers =
    slow: marks tests as slow

# test_sample.py
import pytest

@pytest.mark.slow
def test_long_process():
    assert True
This example shows a database marker for tests needing a database.
PyTest
[pytest]
markers =
    database: marks tests that need database

# test_db.py
import pytest

@pytest.mark.database
def test_db_connection():
    assert True
Sample Program

This test file has two tests marked with smoke and one without any marker.

You can run only smoke tests with pytest -m smoke.

PyTest
# pytest.ini
[pytest]
markers =
    smoke: quick tests to check basic features

# test_smoke.py
import pytest

@pytest.mark.smoke
def test_basic_feature():
    assert 1 + 1 == 2

@pytest.mark.smoke
def test_another_basic():
    assert 'a'.upper() == 'A'


def test_not_marked():
    assert True
OutputSuccess
Important Notes

Always register markers in pytest.ini to keep tests clean and avoid warnings.

You can combine markers with and, or, and not when running tests.

Markers help organize tests and make running specific groups easy and fast.

Summary

Markers label tests for easy grouping and running.

Register markers in pytest.ini to avoid warnings.

Use @pytest.mark.marker_name above test functions to apply markers.