Decorator to skip a test if an optional module is missing
PARAMETER | DESCRIPTION |
module | |
dep_target | Target name for pip installation (e.g. 'test' in pip install ag2[test]) TYPE: Optional[str] DEFAULT: None |
Source code in autogen/import_utils.py
| def skip_on_missing_imports(modules: Union[str, Iterable[str]], dep_target: Optional[str] = None) -> Callable[[T], T]:
"""Decorator to skip a test if an optional module is missing
Args:
module: Module name
dep_target: Target name for pip installation (e.g. 'test' in pip install ag2[test])
"""
missing_modules = get_missing_imports(modules)
# Add pytest.mark.dep_target decorator
# For example, if dep_target is "jupyter-executor" add pytest.mark.jupyter_executor
mark_name = dep_target.replace("-", "_") if dep_target else "openai"
if not missing_modules:
def decorator(o: T) -> T:
import pytest
pytest_mark_o = getattr(pytest.mark, mark_name)(o)
return pytest_mark_o # type: ignore[no-any-return]
else:
def decorator(o: T) -> T:
import pytest
install_target = "" if dep_target is None else f"[{dep_target}]"
pytest_mark_o = getattr(pytest.mark, mark_name)(o)
return pytest.mark.skip( # type: ignore[return-value,no-any-return]
f"Missing module{'s' if len(missing_modules) > 1 else ''}: {', '.join(missing_modules)}. Install using 'pip install ag2{install_target}'"
)(pytest_mark_o)
return decorator
|