Skip to content

Commit bec63f4

Browse files
committed
refactor: reorganize tests into src/tests/integration directory
- Moved all test files from root tests/ to MCPForUnity/UnityMcpServer~/src/tests/integration/ for better organization - Added conftest.py with telemetry and dependency stubs to simplify test setup - Removed redundant path manipulation and module loading code from individual test files
1 parent 4d88c4e commit bec63f4

31 files changed

+141
-574
lines changed

MCPForUnity/UnityMcpServer~/src/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ py-modules = [
3737
"telemetry_decorator",
3838
"unity_connection"
3939
]
40-
packages = ["tools", "resources", "registry"]
40+
packages = ["tools", "resources", "registry", "tests", "tests.integration"]

MCPForUnity/UnityMcpServer~/src/tests/__init__.py

Whitespace-only changes.
File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import sys
3+
import types
4+
5+
# Ensure telemetry is disabled during test collection and execution to avoid
6+
# any background network or thread startup that could slow or block pytest.
7+
os.environ.setdefault("DISABLE_TELEMETRY", "true")
8+
os.environ.setdefault("UNITY_MCP_DISABLE_TELEMETRY", "true")
9+
os.environ.setdefault("MCP_DISABLE_TELEMETRY", "true")
10+
11+
# NOTE: These tests are integration tests for the MCP server Python code.
12+
# They test tools, resources, and utilities without requiring Unity to be running.
13+
# Tests can now import directly from the parent package since they're inside src/
14+
# To run: cd MCPForUnity/UnityMcpServer~/src && uv run pytest tests/integration/ -v
15+
16+
# Stub telemetry modules to avoid file I/O during import of tools package
17+
telemetry = types.ModuleType("telemetry")
18+
def _noop(*args, **kwargs):
19+
pass
20+
class MilestoneType:
21+
pass
22+
telemetry.record_resource_usage = _noop
23+
telemetry.record_tool_usage = _noop
24+
telemetry.record_milestone = _noop
25+
telemetry.MilestoneType = MilestoneType
26+
telemetry.get_package_version = lambda: "0.0.0"
27+
sys.modules.setdefault("telemetry", telemetry)
28+
29+
telemetry_decorator = types.ModuleType("telemetry_decorator")
30+
def telemetry_tool(*dargs, **dkwargs):
31+
def _wrap(fn):
32+
return fn
33+
return _wrap
34+
telemetry_decorator.telemetry_tool = telemetry_tool
35+
sys.modules.setdefault("telemetry_decorator", telemetry_decorator)
36+
37+
# Stub fastmcp module (not mcp.server.fastmcp)
38+
fastmcp = types.ModuleType("fastmcp")
39+
40+
class _DummyFastMCP:
41+
pass
42+
43+
class _DummyContext:
44+
pass
45+
46+
fastmcp.FastMCP = _DummyFastMCP
47+
fastmcp.Context = _DummyContext
48+
sys.modules.setdefault("fastmcp", fastmcp)

tests/test_edit_normalization_and_noop.py renamed to MCPForUnity/UnityMcpServer~/src/tests/integration/test_edit_normalization_and_noop.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
import sys
2-
import pathlib
3-
import importlib.util
4-
5-
6-
ROOT = pathlib.Path(__file__).resolve().parents[1]
7-
SRC = ROOT / "MCPForUnity" / "UnityMcpServer~" / "src"
8-
sys.path.insert(0, str(SRC))
9-
10-
11-
def _load(path: pathlib.Path, name: str):
12-
spec = importlib.util.spec_from_file_location(name, path)
13-
mod = importlib.util.module_from_spec(spec)
14-
spec.loader.exec_module(mod)
15-
return mod
16-
17-
18-
manage_script = _load(SRC / "tools" / "manage_script.py", "manage_script_mod2")
19-
manage_script_edits = _load(
20-
SRC / "tools" / "script_apply_edits.py", "script_apply_edits_mod2")
1+
from tests.integration.test_helpers import DummyContext
212

223

234
class DummyMCP:
@@ -28,9 +9,6 @@ def deco(fn): self.tools[fn.__name__] = fn; return fn
289
return deco
2910

3011

31-
from tests.test_helpers import DummyContext
32-
33-
3412
def setup_tools():
3513
mcp = DummyMCP()
3614
# Import the tools module to trigger decorator registration

tests/test_edit_strict_and_warnings.py renamed to MCPForUnity/UnityMcpServer~/src/tests/integration/test_edit_strict_and_warnings.py

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,4 @@
1-
import sys
2-
import pathlib
3-
import importlib.util
4-
import types
5-
6-
from tests.test_helpers import DummyContext
7-
8-
9-
ROOT = pathlib.Path(__file__).resolve().parents[1]
10-
SRC = ROOT / "MCPForUnity" / "UnityMcpServer~" / "src"
11-
sys.path.insert(0, str(SRC))
12-
13-
# Stub telemetry modules to avoid file I/O during import of tools package
14-
telemetry = types.ModuleType("telemetry")
15-
def _noop(*args, **kwargs):
16-
pass
17-
class MilestoneType:
18-
pass
19-
telemetry.record_resource_usage = _noop
20-
telemetry.record_tool_usage = _noop
21-
telemetry.record_milestone = _noop
22-
telemetry.MilestoneType = MilestoneType
23-
telemetry.get_package_version = lambda: "0.0.0"
24-
sys.modules.setdefault("telemetry", telemetry)
25-
26-
telemetry_decorator = types.ModuleType("telemetry_decorator")
27-
def telemetry_tool(*dargs, **dkwargs):
28-
def _wrap(fn):
29-
return fn
30-
return _wrap
31-
telemetry_decorator.telemetry_tool = telemetry_tool
32-
sys.modules.setdefault("telemetry_decorator", telemetry_decorator)
33-
34-
# stub mcp.server.fastmcp
35-
mcp_pkg = types.ModuleType("mcp")
36-
server_pkg = types.ModuleType("mcp.server")
37-
fastmcp_pkg = types.ModuleType("mcp.server.fastmcp")
38-
39-
40-
class _Dummy:
41-
pass
42-
43-
44-
fastmcp_pkg.FastMCP = _Dummy
45-
fastmcp_pkg.Context = _Dummy
46-
server_pkg.fastmcp = fastmcp_pkg
47-
mcp_pkg.server = server_pkg
48-
sys.modules.setdefault("mcp", mcp_pkg)
49-
sys.modules.setdefault("mcp.server", server_pkg)
50-
sys.modules.setdefault("mcp.server.fastmcp", fastmcp_pkg)
1+
from tests.integration.test_helpers import DummyContext
512

523

534
class DummyMCP:

tests/test_find_in_file_minimal.py renamed to MCPForUnity/UnityMcpServer~/src/tests/integration/test_find_in_file_minimal.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
1-
import sys
2-
import pathlib
3-
import importlib.util
4-
import types
51
import asyncio
62
import pytest
73

8-
from tests.test_helpers import DummyContext
9-
10-
ROOT = pathlib.Path(__file__).resolve().parents[1]
11-
SRC = ROOT / "MCPForUnity" / "UnityMcpServer~" / "src"
12-
sys.path.insert(0, str(SRC))
13-
14-
# Stub telemetry modules to avoid file I/O during import of tools package
15-
telemetry = types.ModuleType("telemetry")
16-
def _noop(*args, **kwargs):
17-
pass
18-
class MilestoneType:
19-
pass
20-
telemetry.record_resource_usage = _noop
21-
telemetry.record_tool_usage = _noop
22-
telemetry.record_milestone = _noop
23-
telemetry.MilestoneType = MilestoneType
24-
telemetry.get_package_version = lambda: "0.0.0"
25-
sys.modules.setdefault("telemetry", telemetry)
26-
27-
telemetry_decorator = types.ModuleType("telemetry_decorator")
28-
def telemetry_tool(*dargs, **dkwargs):
29-
def _wrap(fn):
30-
return fn
31-
return _wrap
32-
telemetry_decorator.telemetry_tool = telemetry_tool
33-
sys.modules.setdefault("telemetry_decorator", telemetry_decorator)
4+
from tests.integration.test_helpers import DummyContext
345

356

367
class DummyMCP:

tests/test_get_sha.py renamed to MCPForUnity/UnityMcpServer~/src/tests/integration/test_get_sha.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
1-
import sys
2-
import pathlib
3-
import importlib.util
4-
5-
6-
ROOT = pathlib.Path(__file__).resolve().parents[1]
7-
SRC = ROOT / "MCPForUnity" / "UnityMcpServer~" / "src"
8-
sys.path.insert(0, str(SRC))
9-
10-
11-
def _load_module(path: pathlib.Path, name: str):
12-
spec = importlib.util.spec_from_file_location(name, path)
13-
if spec is None or spec.loader is None:
14-
raise ImportError(f"Cannot load module {name} from {path}")
15-
mod = importlib.util.module_from_spec(spec)
16-
spec.loader.exec_module(mod)
17-
return mod
18-
19-
20-
manage_script = _load_module(
21-
SRC / "tools" / "manage_script.py", "manage_script_mod")
1+
from tests.integration.test_helpers import DummyContext
222

233

244
class DummyMCP:
@@ -32,9 +12,6 @@ def deco(fn):
3212
return deco
3313

3414

35-
from tests.test_helpers import DummyContext
36-
37-
3815
def setup_tools():
3916
mcp = DummyMCP()
4017
# Import the tools module to trigger decorator registration

tests/test_improved_anchor_matching.py renamed to MCPForUnity/UnityMcpServer~/src/tests/integration/test_improved_anchor_matching.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,9 @@
22
Test the improved anchor matching logic.
33
"""
44

5-
import sys
6-
import pathlib
7-
import importlib.util
5+
import re
86

9-
# add server src to path and load modules
10-
ROOT = pathlib.Path(__file__).resolve().parents[1]
11-
SRC = ROOT / "MCPForUnity" / "UnityMcpServer~" / "src"
12-
sys.path.insert(0, str(SRC))
13-
14-
15-
def load_module(path, name):
16-
spec = importlib.util.spec_from_file_location(name, path)
17-
module = importlib.util.module_from_spec(spec)
18-
spec.loader.exec_module(module)
19-
return module
20-
21-
22-
script_apply_edits_module = load_module(
23-
SRC / "tools" / "script_apply_edits.py", "script_apply_edits_module")
7+
import tools.script_apply_edits as script_apply_edits_module
248

259

2610
def test_improved_anchor_matching():
@@ -41,8 +25,6 @@ def test_improved_anchor_matching():
4125
}
4226
}'''
4327

44-
import re
45-
4628
# Test the problematic anchor pattern
4729
anchor_pattern = r"\s*}\s*$"
4830
flags = re.MULTILINE

0 commit comments

Comments
 (0)