Pytest has the skip and skipif decorators, similar to the Python unittest module (which uses skip and skipIf), which can be found in the documentation here.

Examples from the link can be found here:

@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    ...

import sys
@pytest.mark.skipif(sys.version_info < (3,3), reason="requires python3.3")
def test_function():
    ...

The first example always skips the test, the second example allows you to conditionally skip tests (great when tests depend on the platform, executable version, or optional libraries).

For example, if I want to check if someone has the library pandas installed for a test.

@pytest.mark.skipif(
    not importlib.util.find_spec("pandas"), reason="requires the Pandas library"
)
def test_pandas_function():
    import pandas
    ...
Answer from Alex Huszagh on Stack Overflow
🌐
pytest
docs.pytest.org › en › stable › how-to › skipping.html
How to use skip and xfail to deal with tests that cannot succeed - pytest documentation
You can share skipif markers between modules. Consider this test module: # content of test_mymodule.py import mymodule minversion = pytest.mark.skipif( mymodule.__versioninfo__ < (1, 1), reason="at least mymodule-1.1 required" ) @minversion def test_function(): ...
🌐
pytest
docs.pytest.org › en › stable › reference › reference.html
API Reference - pytest documentation
It is better to use the pytest.mark.skipif marker when possible to declare a test to be skipped under certain conditions like mismatching platforms or dependencies.
🌐
GitHub
github.com › pytest-dev › pytest › discussions › 13311
Executing tests that have skip marks · pytest-dev/pytest · Discussion #13311
# conftest.py import pytest def pytest_addoption(parser): parser.addoption( "--run-skipped", action="store_true", help="turn skip/skipif into xfail at collection time", ) def pytest_configure(config): config.addinivalue_line( "markers", "force_run: added when skip/skipif is coerced", ) def pytest_collection_modifyitems(config, items): if not config.getoption("--run-skipped"): return for item in items: marks = list(getattr(item, "own_markers", [])) if any(m.name in ("skip", "skipif") for m in marks): item.own_markers[:] = [m for m in marks if m.name not in ("skip", "skipif")] item.add_marker(pytest.mark.force_run) item.add_marker(pytest.mark.xfail(reason="was skip/skipif"))
Author   pytest-dev
🌐
O'Reilly
oreilly.com › library › view › pytest-quick-start › 9781789347562 › 3ef5a34f-3d15-4795-a106-9772f4f3ec80.xhtml
@pytest.mark.skipif - pytest Quick Start Guide [Book]
August 29, 2018 - Pytest provides a built-in mark, skipif, that can be used to skip tests based on specific conditions.
Author   Bruno Oliveira
Published   2018
Pages   160
🌐
pytest
docs.pytest.org › en › stable › example › markers.html
Working with custom markers - pytest documentation
$ pytest --markers @pytest.mar... testing this") skips the test. @pytest.mark.skipif(condition, ..., *, reason=...): skip the given test function if any of the conditions evaluate to True....
🌐
Medium
medium.com › @ramanish1992 › pytest-skip-mark-and-other-options-d5ed47f1e05d
PyTest Skip/Mark and Other Options | by Ramkumar R | Jun, 2023 | Medium | Medium
June 4, 2023 - Test Outcomes: • PASSED (.): The test ran successfully. • FAILED (F): The test did not run successfully (or XPASS + strict). • SKIPPED (s): The test was skipped. You can tell pytest to skip a test by using either the @pytest.mark.skip() or pytest.mark.skipif() decorators, discussed in Skipping Tests .
🌐
TestDriven.io
testdriven.io › tips › d7e8ec35-4dc2-48da-b42a-2b5bb3217fec
Tips and Tricks - Skip tests based on a condition in pytest | TestDriven.io
import sys import pytest @pytest.mark.skipif(sys.platform == "win32", reason="Not running on Window") def test_windows(): assert True
Find elsewhere
🌐
pytest
docs.pytest.org › en › 7.1.x › reference › reference.html
API Reference — pytest documentation
It is better to use the pytest.mark.skipif marker when possible to declare a test to be skipped under certain conditions like mismatching platforms or dependencies.
🌐
Jwodder
jwodder.github.io › kbits › posts › pytest-mark-off
Knowledge Bits — Skipping Pytest Tests Unless an Option is Given
December 5, 2021 - This lets us write skipif decorators that skip tests based on whether or not certain command-line options were given. We start out by defining a command-line option in conftest.py that will be used to tell pytest to run otherwise-skipped tests: def pytest_addoption(parser): parser.addoption( ...
🌐
pytest
docs.pytest.org › en › stable › skipping.html
Skip and xfail: dealing with tests that cannot succeed — pytest documentation
import pytest @pytest.mark.parametrize( ("n", "expected"), [ (1, 2), pytest.param(1, 0, marks=pytest.mark.xfail), pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")), (2, 3), (3, 4), (4, 5), pytest.param( 10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k") ), ], ) def test_increment(n, expected): assert n + 1 == expected
🌐
Statology
statology.org › home › how to skip tests conditionally with pytest using skipif
How to Skip Tests Conditionally with pytest Using skipif
October 4, 2024 - Let’s look at a simple test function that uses the skipif decorator. In this example, we will skip a test if the operating system is Windows: import pytest import sys @pytest.mark.skipif(sys.platform == "win32", reason="Test not applicable on Windows") def dont_test_on_windows(): assert 1 + 1 == 2
🌐
pytest
docs.pytest.org › en › 7.1.x › example › markers.html
Working with custom markers — pytest documentation
$ pytest --markers @pytest.mar... testing this") skips the test. @pytest.mark.skipif(condition, ..., *, reason=...): skip the given test function if any of the conditions evaluate to True....
🌐
GitHub
github.com › pytest-dev › pytest › issues › 8384
Stricter arg/kwarg handling in pytest.mark.skip; skip vs. skipif · Issue #8384 · pytest-dev/pytest
March 1, 2021 - The difference between skip / skipif and xfail (but no xfailif) has always bothered me to be honest. Deprecate and warn about using pytest.mark.skip with a positional argument (i.e.
Author   The-Compiler
🌐
GitHub
github.com › pytest-dev › pytest › issues › 5897
pytest.mark.skipif not skiping the test if the condition is True
October 1, 2019 - If any dependant test is failed, I am changing the flag value as True and in next test case I added check in skipif to check the boolean value and skip the test. Example: class demo(): test_flag = False def test_demo1(self): assert 0 == 1 @pytest.mark.skipif(test_flag, reason="Skip the test") def test_demo2(self): assert 1 == 1 #Fixture definitiion def pytest_runtest_protocol(item, nextitem): reports = runtestprotocol(item, nextitem=nextitem) for report in reports: if report.outcome.upper() == "FAILED": if item.name.split("[")[0] in ("test_create_sid"): item.cls.test_flag = True return True ·
Author   BalajiMoorthy12
🌐
Python For All
pythonforall.com › modules › pytest › tsmark
Skipping and Marking Tests in pytest | PythonForAll
February 9, 2026 - import sys @pytest.mark.skipif(sys.version_info < (3, 9), reason="Requires Python 3.9 or higher.") def test_python_version(): assert True · This test will only run if the Python version is 3.9 or higher. You can create custom markers for organizing tests. To avoid warnings, register custom markers in pytest.ini: [pytest] markers = slow: marks tests as slow (deselect with '-m "not slow"') database: marks tests that require database access ·