0
0
PyTesttesting~5 mins

Running tests by marker (-m) in PyTest

Choose your learning style9 modes available
Introduction

Markers help you group tests by labels. Running tests by marker lets you run only specific groups of tests quickly.

You want to run only fast tests before a quick check.
You want to run tests related to a specific feature only.
You want to skip slow tests during development.
You want to run tests that require a database separately.
You want to run tests marked as 'smoke' before deployment.
Syntax
PyTest
pytest -m <marker_name>

Replace <marker_name> with the label you assigned to tests.

You can combine markers with 'and', 'or', 'not' for complex selection.

Examples
Runs only tests marked with @pytest.mark.smoke.
PyTest
pytest -m smoke
Runs tests marked as 'slow' or 'database'.
PyTest
pytest -m "slow or database"
Runs all tests except those marked 'slow'.
PyTest
pytest -m "not slow"
Sample Program

This test file has three tests. Two are marked 'smoke' and one is marked 'slow'.

Running pytest -m smoke will run only the two smoke tests.

PyTest
import pytest

@pytest.mark.smoke
def test_login():
    assert True

@pytest.mark.slow
def test_data_processing():
    assert True

@pytest.mark.smoke
def test_logout():
    assert True
OutputSuccess
Important Notes

Markers must be registered in pytest.ini or you get warnings.

Use quotes around marker expressions with spaces or operators.

Markers help organize tests and speed up running only what you need.

Summary

Markers label tests for easy grouping.

Use pytest -m <marker> to run tests with that label.

You can combine markers with logical operators for flexible test selection.