How do I disable a test using pytest?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
pytest provides several ways to disable (skip) tests: @pytest.mark.skip unconditionally skips a test, @pytest.mark.skipif skips based on a condition, pytest.importorskip skips if a module is missing, and @pytest.mark.xfail marks a test as expected to fail without blocking the test suite. Skipped tests appear as s in the output and are counted separately from passes and failures. These markers are essential for handling platform-specific tests, optional dependencies, and known bugs.
Unconditional Skip
@pytest.mark.skip permanently disables the test. Always provide a reason so teammates know why it was skipped. The reason appears in the output with -v or -rs flags.
Conditional Skip
skipif evaluates the condition at collection time. If true, the test is skipped with the given reason.
Skip if Module Not Available
importorskip attempts to import the module and skips all tests in the file (or test) if it fails. The minversion parameter checks the module's __version__.
Expected Failure (xfail)
xfail lets the test run. If it fails, it is reported as xfail (expected). If it unexpectedly passes, it is reported as xpass. With strict=True, an unexpected pass counts as a test failure.
Skip at Runtime
pytest.skip() called inside a test body skips at runtime after the test has already started. Use this when the skip condition can only be determined during execution.
Skipping in Fixtures
When a fixture calls pytest.skip(), all tests that use that fixture are skipped.
Custom Skip Markers
Custom markers combined with -m let you selectively run or skip categories of tests without modifying the test code.
Common Pitfalls
- Skipping without a reason:
@pytest.mark.skipwithoutreasonmakes it unclear why the test was disabled. Always provide a reason so the skip can be revisited later. - Using skip instead of xfail for known bugs:
skiphides the test entirely.xfailruns the test and alerts you when the bug is fixed (the test unexpectedly passes). Usexfailfor known bugs. - Forgetting to re-enable skipped tests: Skipped tests accumulate over time. Regularly audit tests marked with
skipandskipifto check whether the conditions still apply. - skipif condition evaluated at import time: The condition in
@pytest.mark.skipifruns when the module is imported. Side effects in the condition (like network calls) slow down test collection for all tests. - Commenting out tests instead of using skip: Commented-out tests are invisible in test reports. Use
@pytest.mark.skipso the test appears in the output and can be tracked.
Summary
@pytest.mark.skip(reason="...")unconditionally skips a test@pytest.mark.skipif(condition, reason="...")skips based on runtime conditionspytest.importorskip("module")skips if an optional dependency is missing@pytest.mark.xfail(reason="...")lets the test run but expects it to failpytest.skip("reason")skips at runtime from inside a test body or fixture- Use custom markers with
pytest -m "not slow"to exclude test categories from the command line
Related reading
- How do I disable log messages from the Requests library?
- How do I disable missing docstring warnings at a file-level in Pylint?
- How do I disable TensorFlow's eager execution?
- How do I disable the security certificate check in Python requests
- How do I get my Maven Integration tests to run
- How do I mock a REST template exchange?
- How do I do a not equal in Django queryset filtering?
- How do I do a not equal in Django queryset filtering?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.