How to Use @pytest.mark.skip to Skip Tests in Pytest
Use
@pytest.mark.skip above a test function to skip it during test runs. This decorator tells pytest to ignore the test, optionally with a reason message.Syntax
The @pytest.mark.skip decorator is placed above a test function to skip it. You can add an optional reason argument to explain why the test is skipped.
@pytest.mark.skip(reason="some reason"): Skips the test with a message.def test_function():: The test function to skip.
python
import pytest @pytest.mark.skip(reason="skip this test for now") def test_example(): assert 1 == 1
Example
This example shows a skipped test with a reason. When you run pytest, it will report the test as skipped and show the reason.
python
import pytest @pytest.mark.skip(reason="Not implemented yet") def test_skip_example(): assert 0 == 1 def test_run_example(): assert 2 + 2 == 4
Output
============================= test session starts =============================
collected 2 items
test_sample.py s. [100%]
========================== 1 skipped, 1 passed in 0.03s ==========================
Common Pitfalls
Common mistakes when using @pytest.mark.skip include:
- Forgetting to import
pytest, causing a NameError. - Using
@pytest.mark.skipwithout parentheses when adding a reason (it requires parentheses if reason is given). - Confusing
@pytest.mark.skipwith@pytest.mark.skipif, which skips conditionally.
python
import pytest # Wrong: missing parentheses with reason # @pytest.mark.skip reason="skip this" # def test_wrong(): # assert True # Correct usage @pytest.mark.skip(reason="skip this") def test_correct(): assert True
Quick Reference
Summary tips for @pytest.mark.skip:
- Use
@pytest.mark.skipto skip tests unconditionally. - Always provide a
reasonfor clarity. - Remember to import
pytestbefore using the decorator. - Use
@pytest.mark.skipiffor conditional skipping.
Key Takeaways
Use @pytest.mark.skip above a test function to skip it unconditionally.
Always add a reason argument to explain why the test is skipped.
Import pytest before using @pytest.mark.skip to avoid errors.
Do not confuse @pytest.mark.skip with @pytest.mark.skipif, which skips conditionally.
Skipped tests show as 's' in pytest output with the reason displayed.