Skip to content

Commit 43dec02

Browse files
Simplify test_session_group.py mocking
Remove complex mocking of create_mcp_http_client in the streamablehttp test case. Instead, let the real create_mcp_http_client execute and only verify that streamable_http_client receives the correct parameters including a real httpx.AsyncClient instance. This simplifies the test by: - Removing 13 lines of mock setup code - Removing 14 lines of mock verification code - Removing 3 lines of mock cleanup code - Trusting that create_mcp_http_client works (it has its own tests) The test now focuses on verifying the integration between session_group and streamable_http_client rather than re-testing create_mcp_http_client.
1 parent 4cabf84 commit 43dec02

File tree

1 file changed

+6
-34
lines changed

1 file changed

+6
-34
lines changed

tests/client/test_session_group.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,6 @@ async def test_establish_session_parameterized(
284284
):
285285
with mock.patch("mcp.client.session_group.mcp.ClientSession") as mock_ClientSession_class:
286286
with mock.patch(patch_target_for_client_func) as mock_specific_client_func:
287-
# For streamablehttp, also need to mock create_mcp_http_client
288-
if client_type_name == "streamablehttp":
289-
mock_create_http_client = mock.patch("mcp.client.session_group.create_mcp_http_client")
290-
mock_create_http_client_func = mock_create_http_client.start()
291-
# Mock httpx_client returned by create_mcp_http_client
292-
mock_httpx_client = mock.AsyncMock(name="MockHttpxClient")
293-
mock_httpx_client.__aenter__.return_value = mock_httpx_client
294-
mock_httpx_client.__aexit__ = mock.AsyncMock(return_value=None)
295-
mock_create_http_client_func.return_value = mock_httpx_client
296-
else:
297-
mock_create_http_client = None
298-
mock_create_http_client_func = None
299-
mock_httpx_client = None
300-
301287
mock_client_cm_instance = mock.AsyncMock(name=f"{client_type_name}ClientCM")
302288
mock_read_stream = mock.AsyncMock(name=f"{client_type_name}Read")
303289
mock_write_stream = mock.AsyncMock(name=f"{client_type_name}Write")
@@ -360,24 +346,14 @@ async def test_establish_session_parameterized(
360346
)
361347
elif client_type_name == "streamablehttp":
362348
assert isinstance(server_params_instance, StreamableHttpParameters)
363-
# Verify create_mcp_http_client was called with headers and timeout
349+
# Verify streamable_http_client was called with url, httpx_client, and terminate_on_close
350+
# The httpx_client is created by the real create_mcp_http_client
364351
import httpx
365352

366-
assert mock_create_http_client_func is not None
367-
expected_timeout = httpx.Timeout(
368-
server_params_instance.timeout.total_seconds(),
369-
read=server_params_instance.sse_read_timeout.total_seconds(),
370-
)
371-
mock_create_http_client_func.assert_called_once_with(
372-
headers=server_params_instance.headers,
373-
timeout=expected_timeout,
374-
)
375-
# Verify streamable_http_client was called with url, httpx_client, and terminate_on_close
376-
mock_specific_client_func.assert_called_once_with(
377-
url=server_params_instance.url,
378-
httpx_client=mock_httpx_client,
379-
terminate_on_close=server_params_instance.terminate_on_close,
380-
)
353+
call_args = mock_specific_client_func.call_args
354+
assert call_args.kwargs["url"] == server_params_instance.url
355+
assert call_args.kwargs["terminate_on_close"] == server_params_instance.terminate_on_close
356+
assert isinstance(call_args.kwargs["httpx_client"], httpx.AsyncClient)
381357

382358
mock_client_cm_instance.__aenter__.assert_awaited_once()
383359

@@ -389,7 +365,3 @@ async def test_establish_session_parameterized(
389365
# 3. Assert returned values
390366
assert returned_server_info is mock_initialize_result.serverInfo
391367
assert returned_session is mock_entered_session
392-
393-
# Clean up streamablehttp-specific mock
394-
if mock_create_http_client:
395-
mock_create_http_client.stop()

0 commit comments

Comments
 (0)