Skip to content

Commit f888a0a

Browse files
authored
Merge pull request #121 from wang-boyu/refactor/test-mocks
test: consolidate and reuse test fixtures in conftest files
2 parents 4c0549e + 42ead07 commit f888a0a

16 files changed

+349
-517
lines changed

tests/conftest.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,13 @@
1818
}
1919

2020

21-
class MockCell:
22-
"""Mock cell with coordinate attribute"""
23-
24-
def __init__(self, coordinate):
25-
self.coordinate = coordinate
26-
27-
2821
@pytest.fixture(autouse=True)
2922
def mock_environment():
3023
"""Ensure tests don't depend on real environment variables"""
3124
with patch.dict(
3225
os.environ,
3326
{
34-
"GEMINI_API_KEY": "dummy",
27+
"GEMINI_API_KEY": "test_gemini_key",
3528
"PROVIDER_API_KEY": "test_key",
3629
"OPENAI_API_KEY": "test_openai_key",
3730
},

tests/test_async_memory.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from mesa_llm.llm_agent import LLMAgent
99
from mesa_llm.parallel_stepping import step_agents_parallel
1010
from mesa_llm.reasoning.reasoning import Reasoning
11+
from tests.conftest import build_llm_response
1112

1213
logger = logging.getLogger(__name__)
1314

@@ -53,10 +54,7 @@ async def test_parallel_memory_consolidation(self, mock_agenerate):
5354

5455
async def slow_response(*args, **kwargs):
5556
await asyncio.sleep(1)
56-
mock_resp = MagicMock()
57-
mock_resp.choices = [MagicMock()]
58-
mock_resp.choices[0].message.content = "summary"
59-
return mock_resp
57+
return build_llm_response(content="summary")
6058

6159
mock_agenerate.side_effect = slow_response
6260

tests/test_llm_agent.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515

1616
def test_apply_plan_adds_to_memory(monkeypatch):
17-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
18-
1917
class DummyModel(Model):
2018
def __init__(self):
2119
super().__init__(seed=42)
@@ -70,8 +68,6 @@ def add_agent(self, pos):
7068

7169

7270
def test_generate_obs_with_one_neighbor(monkeypatch):
73-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
74-
7571
class DummyModel(Model):
7672
def __init__(self):
7773
super().__init__(seed=45)
@@ -128,8 +124,6 @@ def add_agent(self, pos, agent_class=LLMAgent):
128124

129125

130126
def test_send_message_updates_both_agents_memory(monkeypatch):
131-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
132-
133127
class DummyModel(Model):
134128
def __init__(self):
135129
super().__init__(seed=45)
@@ -187,8 +181,6 @@ def fake_add_to_memory(*args, **kwargs):
187181

188182
@pytest.mark.asyncio
189183
async def test_aapply_plan_adds_to_memory(monkeypatch):
190-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
191-
192184
class DummyModel(Model):
193185
def __init__(self):
194186
super().__init__(seed=42)
@@ -236,8 +228,6 @@ async def fake_acall_tools(agent, llm_response):
236228

237229
@pytest.mark.asyncio
238230
async def test_agenerate_obs_with_one_neighbor(monkeypatch):
239-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
240-
241231
class DummyModel(Model):
242232
def __init__(self):
243233
super().__init__(seed=45)
@@ -285,8 +275,6 @@ async def fake_aadd_to_memory(*args, **kwargs):
285275

286276
@pytest.mark.asyncio
287277
async def test_async_wrapper_calls_pre_and_post(monkeypatch):
288-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
289-
290278
class CustomAgent(LLMAgent):
291279
async def astep(self):
292280
self.user_called = True
@@ -350,7 +338,6 @@ def _make_agent(model, vision=0, internal_state=None):
350338

351339
def test_safer_cell_access_agent_with_cell_no_pos(monkeypatch):
352340
"""Agent location falls back to cell.coordinate when pos=None."""
353-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
354341
model = Model(seed=42)
355342
agent = _make_agent(model)
356343
agent.pos = None
@@ -364,7 +351,6 @@ def test_safer_cell_access_agent_with_cell_no_pos(monkeypatch):
364351

365352
def test_safer_cell_access_agent_without_cell_or_pos(monkeypatch):
366353
"""Agent location returns None gracefully when neither pos nor cell exists."""
367-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
368354
model = Model(seed=42)
369355
agent = _make_agent(model)
370356
agent.pos = None
@@ -379,7 +365,6 @@ def test_safer_cell_access_agent_without_cell_or_pos(monkeypatch):
379365

380366
def test_safer_cell_access_neighbor_with_cell_no_pos(monkeypatch):
381367
"""Neighbor position uses cell.coordinate when neighbor.pos=None."""
382-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
383368

384369
class GridModel(Model):
385370
def __init__(self):
@@ -413,7 +398,6 @@ def __init__(self):
413398

414399
def test_safer_cell_access_neighbor_without_cell_or_pos(monkeypatch):
415400
"""Neighbor position returns None when neighbor has neither pos nor cell."""
416-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
417401

418402
class GridModel(Model):
419403
def __init__(self):
@@ -448,7 +432,6 @@ def __init__(self):
448432

449433
def test_generate_obs_with_continuous_space(monkeypatch):
450434
"""Agents within vision radius are included; those outside are not."""
451-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
452435

453436
class ContModel(Model):
454437
def __init__(self):
@@ -485,7 +468,6 @@ def __init__(self):
485468

486469
def test_generate_obs_vision_all_agents(monkeypatch):
487470
"""vision=-1 returns all other agents regardless of position."""
488-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
489471

490472
class GridModel(Model):
491473
def __init__(self):
@@ -519,7 +501,6 @@ def __init__(self):
519501

520502
def test_generate_obs_no_grid_with_vision(monkeypatch):
521503
"""When the model has no grid/space, generate_obs falls back to empty neighbors."""
522-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
523504
model = Model(seed=42) # no grid, no space
524505
agents = LLMAgent.create_agents(
525506
model,
@@ -549,7 +530,6 @@ def test_generate_obs_standard_grid_with_vision_radius(monkeypatch):
549530
- The observation includes nearby agents in local_state.
550531
- The SingleGrid neighbor lookup branch is executed.
551532
"""
552-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
553533

554534
class GridModel(Model):
555535
def __init__(self):
@@ -585,7 +565,6 @@ def test_generate_obs_orthogonal_grid_branches(monkeypatch):
585565
Covers Orthogonal grid-specific branches including
586566
cell-based lookup and fallback behavior.
587567
"""
588-
monkeypatch.setenv("GEMINI_API_KEY", "dummy")
589568

590569
class OrthoModel(Model):
591570
def __init__(self):

tests/test_memory/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def episodic_mock_agent(mock_agent, llm_response_factory):
6+
"""Create an episodic-memory-specific variant of the shared mock agent."""
7+
agent = mock_agent
8+
9+
agent.llm.generate.return_value = llm_response_factory(content='{"grade": 3}')
10+
11+
agent.model.steps = 100
12+
agent.model.events = []
13+
return agent

tests/test_memory/memory_utils.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)