Meaning
This decorator conditionally skips a test when a given boolean expression evaluates to True, typically used to omit tests that require optional extensions or libraries not present in the current environment.
Primary Function
Test execution control
Communicative Purpose
Skip a test if a required extension is missing.
Pattern
@pytest.mark.skipif(; , reason=';')
Core Structure
@pytest.mark.skipif(; , reason=';')
Função primária
Test execution control
Propósito comunicativo
Skip a test if a required extension is missing.
Situações de gatilho
Writing unit tests that depend on optional C extensions; running test suite in minimal environments; ensuring CI passes when optional dependencies are not installed.
Contextos
Python projects using pytest for testing; scientific libraries with optional GPU or CUDA bindings; any package with optional extras.
Padrão
@pytest.mark.skipif(; , reason=';')
Estrutura central
@pytest.mark.skipif(; , reason=';')
Slots de substituição
condition: boolean expression (e.g., not HAS_EXT); reason: string explaining why the test is skipped.
Colocados típicos
- pytest fixtures
- test functions
- HAS_EXT flag
- importlib.util.find_spec
- optional dependencies.
Substituições comuns
- @pytest.mark.skip(reason='...') for unconditional skips
- unittest.skipIf
- try/except ImportError inside test body.
Erros comuns
Missing pytest import; using undefined variable in condition; placing decorator after def; forgetting quotes around reason string.
Similar / contraste
@pytest.mark.skip (unconditional skip) vs @pytest.mark.skipif (conditional); @pytest.mark.xfail for expected failures.
Interferências
Coming from unittest: may expect unittest.skipIf decorator; syntax and module differ.
Família do chunk
- pytest.mark.skip
- pytest.mark.xfail
- pytest.mark.parametrize
- pytest.fixture
Nuance
The condition is evaluated at test collection time; if the variable is undefined, a NameError occurs; ensure HAS_EXT is defined in conftest or imported.
Efeito pragmático
Prevents test suite failures due to missing optional dependencies, keeping CI builds green.
Dica de memória
Skip if extension missing.
Nota
The condition is evaluated at test collection time, not at test execution; if any variable used in the condition is undefined at that point, a NameError will be raised and test collection will fail. Ensure flags like HAS_EXT are defined in conftest.py or imported before test collection.
Upgrade path
Use pytest.importorskip(extension) at the start of a test to skip when the extension cannot be imported.
Log in to save chunks.