← Back to libraryQuestion 205 of 468
🐍PythonIntermediate

pytest Markers and conftest.py

📌 Definition:

Markers (@pytest.mark.name) tag tests for selective running (smoke, regression, slow) or behavior (skip, xfail). conftest.py holds shared fixtures and hooks available to all tests in its directory without imports.

📖 Detailed Explanation:

Built-in markers: @pytest.mark.skip / skipif (conditionally skip), @pytest.mark.xfail (expected failure). Custom markers (registered in pytest.ini) let you run subsets: pytest -m smoke. conftest.py is auto-discovered by pytest and is the standard place for shared fixtures, command-line options (pytest_addoption), and hooks (pytest_runtest_setup) — no import needed, and it applies to its folder and subfolders. Together, markers + conftest organize large suites: tag tests by suite, share driver/data fixtures, and select what CI runs at each stage (smoke on PR, full on nightly).

🔑 Key Points:
  • Markers tag tests: skip/skipif/xfail + custom (smoke, slow)
  • Run subsets with pytest -m marker
  • conftest.py shares fixtures/hooks/options automatically (no import)
  • Register custom markers in pytest.ini to avoid warnings
🌍 Real-World Example:

CI runs pytest -m smoke on every pull request (fast subset) and the full suite nightly, using markers to control scope — while a shared driver fixture lives in conftest.py for all specs.

🎯 Scenario-Based Interview Question:

You want fast 'smoke' tests on every PR and the full suite nightly, sharing one browser fixture across all files. How?