Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest use markers and select tests using -m

  • Use the @pytest.mark.name decorator to tag the tests.
import pytest

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

@pytest.mark.security
@pytest.mark.smoke
def test_database_write():
    assert True

@pytest.mark.security
def test_database_forget():
    assert True

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

@pytest.mark.security
def test_ui_forget():
    assert True

def test_unmarked():
    assert True

pytest --collect-only -m security test_by_marker.py
    test_ui_forget
    test_database_write
    test_database_forget
pytest --collect-only -m smoke test_by_marker.py
    test_database_read
    test_ui_access
    test_database_write
  • We need to declare them in the pytest.ini to avoid the warning
[pytest]
markers =
    smoke: Smoke tests
    security: DevSecOps
    # long: Some long running tests