Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Skip a test module with a missing optional dep instead of exiting pytest (#7971)

Daniel Han36020c004a22
diff --git a/tests/test_missing_optional_dep_skips.py b/tests/test_missing_optional_dep_skips.py
new file mode 100644
index 00000000000..074797161c9
--- /dev/null
+++ b/tests/test_missing_optional_dep_skips.py
@@ -0,0 +1,48 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+"""A missing optional dependency must skip one module, not kill the session."""
+
+import subprocess
+import sys
+import textwrap
+
+import pytest
+
+from tests.utils.os_utils import require_python_package
+
+
+_ABSENT = "unsloth_definitely_not_a_real_package_xyz"
+
+
+def _repo_root():
+ import pathlib
+ return pathlib.Path(__file__).resolve().parents[1]
+
+
+def test_a_missing_package_skips_the_module_under_pytest():
+ with pytest.raises(pytest.skip.Exception):
+ require_python_package(_ABSENT)
+
+
+def test_an_installed_package_is_a_no_op():
+ require_python_package("sys", import_name = "sys")
+
+
+def test_the_standalone_script_path_still_exits():
+ # A subprocess, so pytest is genuinely absent from sys.modules, not faked.
+ script = textwrap.dedent(f"""
+ import sys
+ sys.path.insert(0, {str(_repo_root())!r})
+ for name in list(sys.modules):
+ if name == "pytest" or name.startswith("pytest."):
+ del sys.modules[name]
+ from tests.utils.os_utils import require_python_package
+ require_python_package({_ABSENT!r})
+ print("NO EXIT")
+ """)
+ result = subprocess.run(
+ [sys.executable, "-c", script],
+ capture_output = True,
+ text = True,
+ )
+ assert result.returncode == 1, result.stdout + result.stderr
+ assert "NO EXIT" not in result.stdout
diff --git a/tests/utils/os_utils.py b/tests/utils/os_utils.py
index 0dc3d2fbf5e..86bc0c62490 100644
--- a/tests/utils/os_utils.py
+++ b/tests/utils/os_utils.py
@@ -5,6 +5,14 @@ import shutil
import importlib
+def _missing_dependency(message):
+ """Skip under pytest, since exiting at import time aborts the whole session."""
+ if "pytest" in sys.modules:
+ import pytest
+ pytest.skip(message, allow_module_level = True)
+ sys.exit(1)
+
+
def detect_package_manager():
"""Detect the available package manager"""
package_managers = {
@@ -56,7 +64,7 @@ def check_package_installed(package_name, package_manager = None):
def require_package(package_name, executable_name = None):
- """Require a package to be installed, exit if not found"""
+ """Require a package to be installed; skip the module under pytest if not."""
# Executable in PATH is the most reliable signal
if executable_name:
@@ -92,7 +100,7 @@ def require_package(package_name, executable_name = None):
print(f" conda install -c conda-forge {package_name}")
print(f"\nPlease install the required package and run the script again.")
- sys.exit(1)
+ _missing_dependency(f"{package_name} is not installed")
# Usage
@@ -104,7 +112,7 @@ def require_python_package(
import_name = None,
pip_name = None,
):
- """Require a Python package to be installed, exit if not found"""
+ """Require a Python package to be installed; skip the module under pytest if not."""
if import_name is None:
import_name = package_name
if pip_name is None:
@@ -117,6 +125,6 @@ def require_python_package(
print(f" # or with conda:")
print(f" conda install {pip_name}")
print(f"\nAfter installation, run this script again.")
- sys.exit(1)
+ _missing_dependency(f"Python package '{package_name}' is not installed")
else:
print(f"✓ Python package '{package_name}' is installed")