Skip to content

Commit e1592fe

Browse files
author
Yoshihiro Takahara
committed
test: Add main function error handling tests
Add test cases for server main function error handling scenarios: - Test stdio_server error - Test app.run error Improve test coverage of server.py from 87% to 93% Overall coverage improved from 88% to 90%
1 parent f967720 commit e1592fe

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ test = [
2222
"pytest-asyncio>=0.24.0",
2323
"pytest-env>=1.1.0",
2424
"pytest-cov>=6.0.0",
25+
"pytest-mock>=3.12.0",
2526
]
2627
dev = [
2728
"ruff>=0.0.262",

tests/test_server.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
from typing import List
55

66
import pytest
7+
from mcp.server import stdio
78
from mcp.types import TextContent, Tool
9+
from pytest_mock import MockerFixture
810

911
from mcp_text_editor.server import (
12+
app,
1013
call_tool,
1114
edit_contents_handler,
1215
get_contents_handler,
1316
list_tools,
17+
main,
1418
)
1519

1620

@@ -335,3 +339,33 @@ async def test_edit_contents_handler_missing_hash(tmp_path):
335339
edit_results = json.loads(result[0].text)
336340
assert edit_results[str(test_file)]["result"] == "error"
337341
assert "Missing required field: hash" in edit_results[str(test_file)]["reason"]
342+
343+
344+
@pytest.mark.asyncio
345+
async def test_main_stdio_server_error(mocker: MockerFixture):
346+
"""Test main function with stdio_server error."""
347+
# Mock the stdio_server to raise an exception
348+
mock_stdio = mocker.patch.object(stdio, "stdio_server")
349+
mock_stdio.side_effect = Exception("Stdio server error")
350+
351+
with pytest.raises(Exception) as exc_info:
352+
await main()
353+
assert "Stdio server error" in str(exc_info.value)
354+
355+
356+
@pytest.mark.asyncio
357+
async def test_main_run_error(mocker: MockerFixture):
358+
"""Test main function with app.run error."""
359+
# Mock the stdio_server context manager
360+
mock_stdio = mocker.patch.object(stdio, "stdio_server")
361+
mock_context = mocker.MagicMock()
362+
mock_context.__aenter__.return_value = (mocker.MagicMock(), mocker.MagicMock())
363+
mock_stdio.return_value = mock_context
364+
365+
# Mock app.run to raise an exception
366+
mock_run = mocker.patch.object(app, "run")
367+
mock_run.side_effect = Exception("App run error")
368+
369+
with pytest.raises(Exception) as exc_info:
370+
await main()
371+
assert "App run error" in str(exc_info.value)

0 commit comments

Comments
 (0)