Introduction
Custom markers help you label tests so you can run or skip groups of tests easily.
Custom markers help you label tests so you can run or skip groups of tests easily.
import pytest @pytest.mark.marker_name def test_example(): assert True
Markers are added with @pytest.mark.marker_name above test functions.
You can run tests with a marker using pytest -m marker_name.
slow to run separately.import pytest @pytest.mark.slow def test_long_process(): assert True
import pytest @pytest.mark.skipif(condition=True, reason="Not ready") def test_skip(): assert False
import pytest @pytest.mark.feature_x def test_feature_x(): assert 1 + 1 == 2
Here, two tests are marked database and one test is marked api. You can run only database tests with pytest -m database.
import pytest @pytest.mark.database def test_db_connection(): assert True @pytest.mark.api def test_api_response(): assert True @pytest.mark.database def test_db_query(): assert True
Always register custom markers in pytest.ini to avoid warnings.
Use clear marker names to keep tests organized.
Custom markers label tests for easy grouping and running.
Use @pytest.mark.name to add markers.
Run marked tests with pytest -m name.
slow?pytest -m slow?
import pytest
@pytest.mark.slow
def test_a():
assert True
def test_b():
assert True
@pytest.mark.slow
def test_c():
assert False
pytest -m slow doesimport pytest
@pytest.mark.slow()
def test_example():
assert True
database but exclude those also marked as slow. Which pytest command achieves this?Pass the test and this pattern moves unknown