diff --git a/lib/crewai/tests/events/test_llm_usage_event.py b/lib/crewai/tests/events/test_llm_usage_event.py index d0b29c863..7b6322138 100644 --- a/lib/crewai/tests/events/test_llm_usage_event.py +++ b/lib/crewai/tests/events/test_llm_usage_event.py @@ -4,7 +4,7 @@ from unittest.mock import patch import pytest from pydantic import BaseModel -from crewai.events.event_bus import CrewAIEventsBus +from crewai.events.event_bus import crewai_event_bus from crewai.events.types.llm_events import LLMCallCompletedEvent, LLMCallType from crewai.llm import LLM from crewai.llms.base_llm import BaseLLM @@ -203,7 +203,9 @@ class _StubLLM(BaseLLM): class TestEmitCallCompletedEventPassesUsage: @pytest.fixture def mock_emit(self): - with patch.object(CrewAIEventsBus, "emit") as mock: + # Patch the singleton instance; class-level patches are unreliable + # under pytest ``--import-mode=importlib`` / xdist. + with patch.object(crewai_event_bus, "emit") as mock: yield mock @pytest.fixture diff --git a/lib/crewai/tests/test_llm_streaming_finish_reason.py b/lib/crewai/tests/test_llm_streaming_finish_reason.py index ff8a94d4e..b44a732b2 100644 --- a/lib/crewai/tests/test_llm_streaming_finish_reason.py +++ b/lib/crewai/tests/test_llm_streaming_finish_reason.py @@ -11,22 +11,33 @@ from unittest.mock import patch import pytest -from crewai.events.event_bus import CrewAIEventsBus +from crewai.events.event_bus import crewai_event_bus from crewai.events.types.llm_events import LLMCallCompletedEvent from crewai.llm import LLM @pytest.fixture def mock_emit(): - with patch.object(CrewAIEventsBus, "emit") as mock: + # Patch the singleton instance (not the class). Class-level patches are + # unreliable under pytest ``--import-mode=importlib`` / xdist because the + # test and ``crewai.llm`` can observe different class objects. + with patch.object(crewai_event_bus, "emit") as mock: yield mock +def _event_from_call(call) -> object | None: + if "event" in call.kwargs: + return call.kwargs["event"] + if len(call.args) >= 2: + return call.args[1] + return None + + def _completed_event(mock_emit) -> LLMCallCompletedEvent: matches = [ - call.kwargs["event"] + event for call in mock_emit.call_args_list - if isinstance(call.kwargs.get("event"), LLMCallCompletedEvent) + if isinstance((event := _event_from_call(call)), LLMCallCompletedEvent) ] assert matches, "expected an LLMCallCompletedEvent to be emitted" assert len(matches) == 1, f"expected one completed event, got {len(matches)}"