Skip to content

Commit 839562e

Browse files
test: enhance mock specifications in various test files for better clarity and error prevention
1 parent 626ba5b commit 839562e

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/tests/backend/test_app.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pytest
1414
import sys
1515
import os
16-
from unittest.mock import Mock, AsyncMock, patch, MagicMock
16+
from unittest.mock import Mock, AsyncMock, patch, MagicMock, NonCallableMock
1717
from types import ModuleType
1818

1919
# Environment variables are set by conftest.py, but ensure they're available
@@ -36,7 +36,8 @@
3636
os.environ.setdefault("AZURE_OPENAI_RAI_DEPLOYMENT_NAME", "test-rai-deployment")
3737

3838
# Check if v4 has been mocked by another test file (prevents import errors)
39-
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], (Mock, MagicMock))
39+
# Use NonCallableMock to catch all mock subclasses (Mock, MagicMock, etc.)
40+
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], NonCallableMock)
4041
if _v4_is_mocked:
4142
# Skip this module - v4 has been mocked by another test file
4243
pytest.skip(

src/tests/backend/v4/orchestration/helper/test_plan_to_mplan_converter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
import unittest
1313
import sys
14-
from unittest.mock import Mock, MagicMock
14+
from unittest.mock import Mock, MagicMock, NonCallableMock
1515

1616
import pytest
1717

1818
# Check if v4 has been mocked by another test file (prevents import errors)
19-
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], (Mock, MagicMock))
20-
_v4_models_is_mocked = 'v4.models' in sys.modules and isinstance(sys.modules['v4.models'], (Mock, MagicMock))
19+
# Use NonCallableMock to catch all mock subclasses (Mock, MagicMock, etc.)
20+
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], NonCallableMock)
21+
_v4_models_is_mocked = 'v4.models' in sys.modules and isinstance(sys.modules['v4.models'], NonCallableMock)
2122
if _v4_is_mocked or _v4_models_is_mocked:
2223
pytest.skip(
2324
"Skipping test_plan_to_mplan_converter.py: v4 module has been mocked by another test file. "

src/tests/backend/v4/orchestration/test_orchestration_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ def test_extract_response_text_object_with_empty_text(self):
924924

925925
def test_extract_response_text_agent_executor_response_with_agent_response(self):
926926
"""Test extracting text from AgentExecutorResponse with agent_response.text."""
927-
agent_resp = Mock()
927+
agent_resp = Mock(spec=['text'])
928928
agent_resp.text = "Agent executor response"
929929

930930
executor_resp = Mock(spec=['agent_response'])
@@ -935,7 +935,7 @@ def test_extract_response_text_agent_executor_response_with_agent_response(self)
935935

936936
def test_extract_response_text_agent_executor_response_fallback_to_conversation(self):
937937
"""Test extracting text from AgentExecutorResponse falling back to full_conversation."""
938-
agent_resp = Mock()
938+
agent_resp = Mock(spec=['text'])
939939
agent_resp.text = None
940940

941941
last_msg = MockChatMessage("Last conversation message")
@@ -949,7 +949,7 @@ def test_extract_response_text_agent_executor_response_fallback_to_conversation(
949949

950950
def test_extract_response_text_agent_executor_response_empty_conversation(self):
951951
"""Test extracting text from AgentExecutorResponse with empty conversation."""
952-
agent_resp = Mock()
952+
agent_resp = Mock(spec=['text'])
953953
agent_resp.text = None
954954

955955
executor_resp = Mock(spec=['agent_response', 'full_conversation'])

0 commit comments

Comments
 (0)