Skip to content

Commit 4b18790

Browse files
authored
fix: pytest coroutine warnings (#227)
Root cause**: All three warnings came from tests of the `main()` function, where `asyncio.run` was mocked but `run_proxy(args)` still created coroutines that were never awaited: 1. **`test_main_default`** in `test_main.py` — `@patch('mcp_proxy_for_aws.server.run_proxy')` auto-detected the async function and created an `AsyncMock`. Calling it produced a coroutine passed to the mocked `asyncio.run`, which never awaited it. 2. **`test_main_function`** in `test_server.py` — `run_proxy` wasn't patched at all, so the real `async def run_proxy(args)` created a real coroutine that the mocked `asyncio.run` discarded. 3. **`test_main_error_handling`** in `test_server.py` — Same issue as **Fix**: Added `@patch('mcp_proxy_for_aws.server.run_proxy', new_callable=Mock)` to all three tests. Using `new_callable=Mock` forces a regular `Mock` instead of an `AsyncMock`, so calling `mock_run_proxy(args)` returns a plain `MagicMock` (not a coroutine), which is safely passed to the mocked `asyncio.run` without any unawaited coroutine issues.
1 parent c950d74 commit 4b18790

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

tests/unit/test_main.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
"""Tests for the main function in server.py."""
1616

1717
from mcp_proxy_for_aws.server import main
18-
from unittest.mock import AsyncMock, patch
18+
from unittest.mock import Mock, patch
1919

2020

2121
class TestMain:
2222
"""Tests for the main function."""
2323

2424
@patch('mcp_proxy_for_aws.server.asyncio.run')
25-
@patch('mcp_proxy_for_aws.server.run_proxy')
25+
@patch('mcp_proxy_for_aws.server.run_proxy', new_callable=Mock)
2626
@patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com'])
2727
def test_main_default(self, mock_run_proxy, mock_asyncio_run):
2828
"""Test main function with default arguments."""
29-
# Mock run_proxy as async
30-
mock_run_proxy.return_value = AsyncMock()
31-
3229
# Mock asyncio.run to avoid actual execution
3330
mock_asyncio_run.return_value = None
3431

tests/unit/test_server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,9 @@ def test_parse_args_custom(self):
364364
assert args.retries == 5
365365

366366
@patch('mcp_proxy_for_aws.server.asyncio.run')
367+
@patch('mcp_proxy_for_aws.server.run_proxy', new_callable=Mock)
367368
@patch('sys.argv', ['test', 'https://test.example.com'])
368-
def test_main_function(self, mock_asyncio_run):
369+
def test_main_function(self, mock_run_proxy, mock_asyncio_run):
369370
"""Test that main function runs server correctly."""
370371
# Arrange
371372
mock_asyncio_run.return_value = None
@@ -377,8 +378,9 @@ def test_main_function(self, mock_asyncio_run):
377378
mock_asyncio_run.assert_called_once()
378379

379380
@patch('mcp_proxy_for_aws.server.asyncio.run')
381+
@patch('mcp_proxy_for_aws.server.run_proxy', new_callable=Mock)
380382
@patch('sys.argv', ['test', 'https://test.example.com'])
381-
def test_main_error_handling(self, mock_asyncio_run):
383+
def test_main_error_handling(self, mock_run_proxy, mock_asyncio_run):
382384
"""Test that main function handles errors gracefully."""
383385
# Arrange
384386
mock_asyncio_run.side_effect = Exception('Test error')

0 commit comments

Comments
 (0)