0
0
Selenium Pythontesting~5 mins

Markers for categorization in Selenium Python

Choose your learning style9 modes available
Introduction

Markers help you group and label tests so you can run or skip certain tests easily.

You want to run only fast tests before a quick check.
You want to skip tests that need a database when it's not available.
You want to run tests related to login separately from others.
You want to mark tests that are still under development.
You want to organize tests by feature or priority.
Syntax
Selenium Python
@pytest.mark.marker_name
def test_function():
    pass

Use @pytest.mark.marker_name above your test function to add a marker.

You can use multiple markers by stacking them.

Examples
This test is marked as 'smoke' to run quick checks.
Selenium Python
@pytest.mark.smoke
def test_login():
    assert True
This test will be skipped during runs.
Selenium Python
@pytest.mark.skip
def test_not_ready():
    assert False
This test has two markers: 'database' and 'slow'.
Selenium Python
@pytest.mark.database
@pytest.mark.slow
def test_data_load():
    assert True
Sample Program

This script has three tests with different markers. The 'test_skip_example' will be skipped. You can run tests by marker using pytest options.

Selenium Python
import pytest

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

@pytest.mark.skip
def test_skip_example():
    assert False

@pytest.mark.slow
def test_long_process():
    assert sum(range(5)) == 10
OutputSuccess
Important Notes

To run tests with a specific marker, use pytest -m marker_name.

To skip tests with a marker, use pytest -m 'not marker_name'.

Remember to register custom markers in pytest.ini to avoid warnings.

Summary

Markers label tests to organize and control which tests run.

You add markers using @pytest.mark.marker_name above test functions.

Markers help run, skip, or group tests easily.