0
0
PyTesttesting~5 mins

Registering markers in pytest.ini

Choose your learning style9 modes available
Introduction

Registering markers helps pytest recognize custom labels for tests. This avoids warnings and keeps tests organized.

You want to group tests by features like 'slow' or 'database'.
You need to run only specific types of tests using markers.
You want to avoid pytest warnings about unknown markers.
You want to document what each custom marker means.
You are sharing tests with a team and want consistent marker usage.
Syntax
PyTest
[pytest]
markers =
    marker_name: description of what this marker means
    another_marker: description here

Each marker is listed under the markers section in pytest.ini.

Descriptions help others understand the purpose of each marker.

Examples
This example registers two markers: slow and database with descriptions.
PyTest
[pytest]
markers =
    slow: marks tests as slow to run
    database: marks tests that need database access
Here, ui and api markers are registered for different test categories.
PyTest
[pytest]
markers =
    ui: marks tests related to user interface
    api: marks tests for API endpoints
Sample Program

This shows how to register a smoke marker in pytest.ini and use it in a test.

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

# test_sample.py
import pytest

@pytest.mark.smoke
def test_example():
    assert 1 + 1 == 2
OutputSuccess
Important Notes

Always register custom markers to avoid pytest warnings about unknown markers.

Descriptions in pytest.ini help teammates understand marker purpose.

You can run tests with a marker using pytest -m marker_name.

Summary

Register markers in pytest.ini under the markers section.

This keeps tests organized and avoids warnings.

Use markers to run specific groups of tests easily.