Framework Mode - Running tests by marker (-m)
Folder Structure
project-root/
├── tests/
│ ├── test_login.py
│ ├── test_signup.py
│ ├── test_profile.py
│ └── __init__.py
├── pytest.ini
├── conftest.py
└── utils/
└── helpers.py
project-root/
├── tests/
│ ├── test_login.py
│ ├── test_signup.py
│ ├── test_profile.py
│ └── __init__.py
├── pytest.ini
├── conftest.py
└── utils/
└── helpers.py
Use pytest.ini to declare custom markers to avoid warnings and organize tests:
[pytest]
markers =
smoke: quick smoke tests
regression: full regression tests
slow: tests that take longer time
Run tests by marker using the -m option:
pytest -m smoke
This runs only tests marked with @pytest.mark.smoke.
Combine markers with and, or, and not:
pytest -m "smoke and not slow"
Use environment variables or command line options to select markers dynamically if needed.
pytest -m smoke --junitxml=reports/smoke-results.xml
pytest.ini to keep the framework clean and avoid warnings.and, or, and not to fine-tune test selection.Where in this folder structure would you add a new test file for API tests that you want to mark as regression?
-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