fix(tests): mock spacy in plugin tests to fix externally-managed-environment failures

This commit is contained in:
Artem Akymenko
2026-07-16 10:02:01 +03:00
parent 1268a83cff
commit ef07a8b5b2
2 changed files with 21 additions and 1 deletions
+18
View File
@@ -12,6 +12,7 @@ from __future__ import annotations
import logging import logging
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
from unittest.mock import patch
import pytest import pytest
@@ -147,6 +148,23 @@ def engine_config() -> Any:
return EngineConfig(device="cpu") return EngineConfig(device="cpu")
@pytest.fixture(autouse=True)
def _mock_kokoro_pipeline():
"""Prevent real KPipeline initialization during generic plugin tests.
The real KPipeline requires spacy model downloads which aren't available
in externally-managed environments. Mock spacy's download and load so
the engine contract can be tested without heavy dependencies.
"""
from unittest.mock import MagicMock
mock_nlp = MagicMock()
with patch("spacy.cli.download"), \
patch("spacy.load", return_value=mock_nlp):
yield
@pytest.fixture @pytest.fixture
def create_engine(loaded_plugin, host_context, engine_config): def create_engine(loaded_plugin, host_context, engine_config):
"""Create an engine instance from a loaded plugin. """Create an engine instance from a loaded plugin.
+3 -1
View File
@@ -38,8 +38,10 @@ from tests.contracts.engine_contract import EngineContractMixin
def _kokoro_available() -> bool: def _kokoro_available() -> bool:
try: try:
from kokoro import KPipeline # type: ignore[import-not-found] from kokoro import KPipeline # type: ignore[import-not-found]
import spacy
spacy.load("en_core_web_sm")
return True return True
except ImportError: except (ImportError, OSError):
return False return False