Disable individual Python unit tests temporarily
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Temporarily disabling a test is sometimes the right move, but it should be visible, explicit, and easy to undo. In Python, the safest options are skip markers and decorators provided by unittest or pytest, because they leave a trace in test output instead of silently removing coverage. That visibility is the main difference between responsible temporary suppression and quietly losing a test.
Use Skip Decorators in unittest
The standard library unittest module includes built-in skip support:
When the test suite runs, the test is reported as skipped with the given reason. That is much better than commenting the test out, because the skipped state is visible to everyone reading CI output.
You can also make the skip conditional:
Use conditional skips when the problem is environment-specific rather than universally broken.
Use pytest Markers for the Same Job
If your test suite uses pytest, the equivalent pattern is @pytest.mark.skip or @pytest.mark.skipif:
Conditional version:
This keeps the intent close to the test and makes the temporary disablement obvious in code review.
Know When xfail Is Better Than skip
Sometimes you do not want to disable the test completely. You want the test to run, but you expect it to fail for now. In pytest, that is what xfail is for:
skip means "do not run this test." xfail means "run it, but treat the current failure as expected." If you still want signal when the behavior unexpectedly improves, xfail is often the better choice.
Keep Temporary Disables Temporary
A skipped test is a debt marker. Treat it like one. Good habits include:
- include a reason
- link to a ticket or bug id when possible
- review skipped tests regularly
- remove the skip as soon as the underlying issue is fixed
If you need to exclude a test only for one local run, command-line selection is sometimes better than editing the test file. In pytest, for example, you can use -k to narrow the run temporarily instead of committing a skip marker. That keeps the repository clean while still letting you move quickly during local debugging.
Common Pitfalls
The biggest mistake is commenting out the test or deleting it temporarily. That hides the missing coverage from CI and from future readers.
Another issue is adding a skip with no reason. A week later, nobody remembers whether the test was flaky, blocked by infrastructure, or exposing a real product bug.
Using skip when you really want xfail is also common. If the test should still execute and report when the bug disappears, skipping it throws away that signal.
Finally, do not let skipped tests accumulate indefinitely. Temporary skips that never get revisited become permanent blind spots.
Summary
- Use
@unittest.skipor@pytest.mark.skipto disable a specific test visibly. - Use conditional skip helpers when the failure depends on platform or environment.
- Prefer
xfailwhen the test should still run but a failure is currently expected. - Always include a reason and ideally a bug reference.
- Avoid commenting out tests, because that hides the problem instead of tracking it.

