MCPcopy
hub / github.com/openai/swarm / test_tool_call

Function test_tool_call

tests/test_core.py:31–67  ·  view source on GitHub ↗
(mock_openai_client: MockOpenAIClient)

Source from the content-addressed store, hash-verified

29
30
31def test_tool_call(mock_openai_client: MockOpenAIClient):
32 expected_location = "San Francisco"
33
34 # set up mock to record function calls
35 get_weather_mock = Mock()
36
37 def get_weather(location):
38 get_weather_mock(location=location)
39 return "It's sunny today."
40
41 agent = Agent(name="Test Agent", functions=[get_weather])
42 messages = [
43 {"role": "user", "content": "What's the weather like in San Francisco?"}
44 ]
45
46 # set mock to return a response that triggers function call
47 mock_openai_client.set_sequential_responses(
48 [
49 create_mock_response(
50 message={"role": "assistant", "content": ""},
51 function_calls=[
52 {"name": "get_weather", "args": {"location": expected_location}}
53 ],
54 ),
55 create_mock_response(
56 {"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT}
57 ),
58 ]
59 )
60
61 # set up client and run
62 client = Swarm(client=mock_openai_client)
63 response = client.run(agent=agent, messages=messages)
64
65 get_weather_mock.assert_called_once_with(location=expected_location)
66 assert response.messages[-1]["role"] == "assistant"
67 assert response.messages[-1]["content"] == DEFAULT_RESPONSE_CONTENT
68
69
70def test_execute_tools_false(mock_openai_client: MockOpenAIClient):

Callers

nothing calls this directly

Calls 5

runMethod · 0.95
AgentClass · 0.90
create_mock_responseFunction · 0.90
SwarmClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…