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 OverflowVideos
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
...
The skip decorator would do the job:
@pytest.mark.skip(reason="no way of currently testing this")
def test_func_one():
# ...
(reason argument is optional, but it is always a good idea to specify why a test is skipped).
There is also skipif() that allows to disable a test if some specific condition is met.
These decorators can be applied to methods, functions or classes.
To skip all tests in a module, define a global pytestmark variable:
# test_module.py
pytestmark = pytest.mark.skipif(...)
Create a conftest.py with the following contents:
import pytest
import _pytest.skipping
def pytest_addoption(parser):
parser.addoption(
"--no-skips",
action="store_true",
default=False, help="disable skip marks")
@pytest.hookimpl(tryfirst=True)
def pytest_cmdline_preparse(config, args):
if "--no-skips" not in args:
return
def no_skip(*args, **kwargs):
return
_pytest.skipping.skip = no_skip
the use --no-skip in command line to run all testcases even if some testcases with pytest.mark.skip decorator
Here is a short working solution based on the answer from hoefling:
Add in your conftest.py:
from typing import Any, List
from typing_extensions import Final
NO_SKIP_OPTION: Final[str] = "--no-skip"
def pytest_addoption(parser):
parser.addoption(NO_SKIP_OPTION, action="store_true", default=False, help="also run skipped tests")
def pytest_collection_modifyitems(config,
items: List[Any]):
if config.getoption(NO_SKIP_OPTION):
for test in items:
test.own_markers = [marker for marker in test.own_markers if marker.name not in ('skip', 'skipif')]