Custom Markers
Markers are labels you attach to tests so you can run groups of them.
ini
# pytest.ini
[pytest]
markers =
smoke: fast checks of the most important features
regression: full suite
login: login related tests
slow: tests that take more than a minute
python
import pytest
@pytest.mark.smoke
def test_homepage_loads(driver): ...
@pytest.mark.regression
@pytest.mark.login
def test_password_reset(driver): ...
@pytest.mark.smoke
@pytest.mark.login
class TestLogin:
def test_valid(self, driver): ...
def test_invalid(self, driver): ...
bash
pytest -m smoke
pytest -m "regression and not slow"
pytest -m "login or smoke"
pytest --strict-markers # error on misspelled markers
| Suite | Purpose | When |
| Smoke | Is the app basically alive? 5 to 20 tests | After every deployment, a few minutes |
| Sanity | Check a specific fix | After a bug fix |
| Regression | Everything still works? | Nightly or before release |
skip and skipif
python
import sys
@pytest.mark.skip(reason="Feature not implemented yet")
def test_export_pdf(driver): ...
@pytest.mark.skipif(sys.platform == "darwin", reason="Not supported on macOS")
def test_windows_only(driver): ...
def test_dynamic(driver):
if driver.capabilities["browserName"] == "firefox":
pytest.skip("Known issue in Firefox")
xfail: Expected Failures
python
@pytest.mark.xfail(reason="Bug JIRA-123: sorting is broken")
def test_sort_by_price(driver): ...
If it fails the result is xfail (not a failure). If it unexpectedly passes the result is xpass, a signal that the bug may be fixed.
Rerun Flaky Tests (plugin)
bash
pip install pytest-rerunfailures
pytest --reruns 2 --reruns-delay 1
python
@pytest.mark.flaky(reruns=2)
def test_unstable_widget(driver): ...
Fix, do not hide: Reruns reduce noise but do not repair the cause. Track reruns and fix those tests.
Timeouts
bash
pip install pytest-timeout
pytest --timeout=120
Order and Dependencies
Tests should not depend on each other. If you must control order for a special flow (for example an end-to-end scenario), use one test with several steps instead of ordering many tests.
Run Many Tests by Folder
text
tests/
smoke/
login/
checkout/
api/
bash
pytest tests/login
pytest tests/smoke tests/checkout
Conclusion
Use markers to build smoke and regression suites, skip and xfail for known issues, and a rerun plugin only as a temporary helper. Keep every test independent.
Quick Summary: @pytest.mark.smoke + pytest -m smoke; skip/xfail for known issues.