Junior — Middle
How can a specific test be skipped or disabled in PyTest?
sobes.tech AI
Answer from AI
In PyTest, you can skip or disable a test in several ways:
- Use the
@pytest.mark.skipdecorator to completely skip a test:
import pytest
@pytest.mark.skip(reason="Reason for skipping")
def test_example():
assert False
- Use
@pytest.mark.skipiffor conditional skipping:
import pytest
import sys
@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on Windows")
def test_unix_only():
assert True
- Call
pytest.skip()inside the test for dynamic skipping:
import pytest
def test_dynamic_skip():
if not some_condition():
pytest.skip("Condition not met")
assert True
Thus, you can flexibly control test execution based on conditions or reasons.