Run pytest tests filtered by marker
Preconditions (2)
✅ Expected Result: Only tests with the 'smoke' marker run and pass or fail accordingly
import pytest @pytest.mark.smoke def test_smoke_1(): assert 1 + 1 == 2 @pytest.mark.smoke def test_smoke_2(): assert 'a' * 3 == 'aaa' @pytest.mark.regression def test_regression_1(): assert 5 > 2 @pytest.mark.regression def test_regression_2(): assert len([1, 2, 3]) == 3 # To run only smoke tests, execute in terminal: # pytest -m smoke # Expected output: only test_smoke_1 and test_smoke_2 run
The code defines four test functions with two different markers: smoke and regression.
Using @pytest.mark.smoke marks tests as smoke tests, and @pytest.mark.regression marks others as regression tests.
Running pytest -m smoke in the terminal runs only the tests marked with smoke. This filters tests by marker, so tests without the marker are skipped.
This approach helps run specific groups of tests quickly, like smoke tests for basic checks.
Add a new marker 'fast' and create two tests with it. Run tests filtered by 'fast' marker.
-m option do when running pytest?-m option-m option runs only tests that have the specified marker label.slow?-m, not --marker or others.pytest -m slow to run tests marked as slow.fast and db:
@pytest.mark.fast def test_a(): pass @pytest.mark.db def test_b(): pass @pytest.mark.fast def test_c(): passWhat tests run with the command
pytest -m "fast and not db"?fast AND NOT marked with db.test_a and test_c have fast only; test_b has db only.pytest -m slow but no tests run, even though some tests have @pytest.mark.slow. What is the most likely cause?pytest.ini to avoid warnings and ensure correct filtering.api, ui, and db. You want to run tests that are either api or ui, but not db. Which command achieves this?api or ui together.(api or ui) and excludes db with and not db.Pass the test and this pattern moves unknown