fix: unit test refactoring with conftest and more mocks

ref: N25B-205
This commit is contained in:
Björn Otgaar
2025-10-28 13:26:33 +01:00
parent c75f5de97c
commit 423309e063
7 changed files with 142 additions and 27 deletions

View File

@@ -0,0 +1,57 @@
import sys
from unittest.mock import MagicMock, AsyncMock
class DummyCyclicBehaviour:
async def run(self):
pass
def kill(self):
self.is_killed = True
return None
class DummyAgent:
def __init__(self, jid=None, password=None, *_, **__):
self.jid = jid
self.password = password
self.behaviours = []
async def start(self):
return AsyncMock()
def add_behaviour(self, behaviour):
behaviour.agent = self
self.behaviours.append(behaviour)
async def stop(self):
pass
def pytest_configure(config):
"""
This hook runs at the start of the pytest session, before any tests are
collected. It mocks heavy or unavailable modules to prevent ImportErrors.
"""
# --- Mock spade and spade-bdi ---
mock_spade = MagicMock()
mock_spade.agent = MagicMock(Agent=DummyAgent)
mock_spade.behaviour = MagicMock(CyclicBehaviour=DummyCyclicBehaviour)
mock_spade_bdi = MagicMock()
mock_spade_bdi.bdi = MagicMock()
mock_spade.agent.Message = MagicMock()
sys.modules["spade"] = mock_spade
sys.modules["spade.agent"] = mock_spade.agent
sys.modules["spade.behaviour"] = mock_spade.behaviour
sys.modules["spade_bdi"] = mock_spade_bdi
sys.modules["spade_bdi.bdi"] = mock_spade_bdi.bdi
# --- Mock the config module to prevent Pydantic ImportError ---
mock_config_module = MagicMock()
# The code under test does `from ... import settings`, so our mock module
# must have a `settings` attribute. We'll make it a MagicMock so we can
# configure it later in our tests using mocker.patch.
mock_config_module.settings = MagicMock()
sys.modules["control_backend.core.config"] = mock_config_module