Replies: 3 comments 5 replies
-
The builtin way is to call def test_something():
extra_name = pytest.importorskip("extra_name")
# Test logic here
pass |
Beta Was this translation helpful? Give feedback.
-
Personally I'd be in favor of a Perhaps it could even have a version check (via However, I see no reason why this needs to start in the core rather than a plugin. The usual approach for this is that someone starts with a plugin, and once the details are figured out and there is some usage behind it, it's worth a discussion whether to merge the functionality into pytest core (like happened with faulthandler or logging). I'm completely lost in the example implementation of the OP though. Where is |
Beta Was this translation helpful? Give feedback.
-
For what it's worth, here is how I currently would implement a helper like what you describe: import pytest
import importlib
def skip_if_missing(name: str):
try:
importlib.import_module(name)
except ModuleNotFoundError:
return pytest.mark.skip(f"{name} missing")
return lambda func: func
@skip_if_missing("extra_name")
def test_something():
pass |
Beta Was this translation helpful? Give feedback.
-
I have a Python package with optional dependencies defined in pyproject.toml. I want to run pytest while ensuring that specific tests are executed only if the relevant optional dependencies are installed. Otherwise, these tests should be skipped.
For example, given optional dependencies like this in pyproject.toml:
I want to use a decorator like this:
My current implementation idea looks like this:
It feels cumbersome and could be improved.
Moved from: https://stackoverflow.com/questions/79361888/how-to-gracefully-skip-pytest-tests-if-optional-dependencies-are-not-installed
Beta Was this translation helpful? Give feedback.
All reactions