Use pytest markers to categorize and selectively run tests
Preconditions (2)
✅ Expected Result: Tests run according to the markers specified in the pytest command line options
import pytest @pytest.mark.smoke def test_login(): assert True @pytest.mark.smoke def test_logout(): assert True @pytest.mark.regression def test_profile(): assert True # To run smoke tests only, use command: # pytest -m smoke # To skip smoke tests, use command: # pytest -m "not smoke"
This script defines three test functions. Two are marked with @pytest.mark.smoke and one with @pytest.mark.regression. Markers help group tests.
Using pytest -m smoke runs only tests marked 'smoke'. Using pytest -m "not smoke" skips those tests.
This shows how markers categorize tests and control which tests run.
Add a new marker 'slow' to one test and run tests excluding 'slow' tests
pytest -m "slow and not network"?
@pytest.mark.slow def test_a(): pass @pytest.mark.network def test_b(): pass @pytest.mark.slow @pytest.mark.network def test_c(): pass def test_d(): pass
@pytest.mark.skipif(sys.platform == 'win32')
def test_func():
assert TruePass the test and this pattern moves unknown