Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Pytest & fixturesMiddleCommonPractical

How do pytest markers, `-m` selection, `skip` and `xfail` differ, and how would you use them in a real automation suite?

Answer

Custom markers attach metadata such as `smoke`, `api` or `slow` to tests and let CI select subsets with `pytest -m`; they should be registered so typos do not silently create new marks. `skip` means the test should not run under the current condition, while `xfail` documents a known expected failure and can be made strict so an unexpected pass is visible. Use markers to define execution slices, not to hide unstable tests indefinitely, and keep reasons for skip/xfail explicit and reviewable.

Strong answer includes

  • distinguishes metadata and selection from execution outcomes
  • explains skip versus expected failure and the value of strict xfail
  • registers custom markers and avoids using them as permanent flakiness hiding

Practical example

A pull-request job runs

Python example
pytest -m "smoke and not external"

, while a nightly job runs the full regression set. A test blocked by an unavailable third-party sandbox uses `skipif` with a clear condition; a test for a confirmed product defect uses a ticket-linked strict `xfail` so the team notices when the defect is fixed.

#pytest#markers#xfail#test-selection

Sources

pytest documentationpytest · Fixture scope, parametrization, markers and plugin architecturepytest Interview QuestionsAssertHired · 2026 Python QA/SDET recurrence signal for pytest fixtures, markers, monkeypatch, xdist, reporting and framework structure