Skip to content

TestAgent

autogen.testing.test_agent.TestAgent #

TestAgent(agent, messages=(), *, suppress_messages_end=False)

A context manager for testing ConversableAgent instances with predefined messages.

This class allows you to temporarily replace an agent's LLM client with a fake client that returns predefined messages. It's useful for testing agent behavior without making actual API calls.

ATTRIBUTE DESCRIPTION
agent

The agent to be tested.

TYPE: ConversableAgent

messages

An iterable of messages to be returned by the fake client.

TYPE: Iterable[str | dict[str, Any]]

suppress_messages_end

Whether to suppress StopIteration exceptions from the fake client.

TYPE: bool

Example

with TestAgent(agent, ["Hello", "How are you?"]) as test_agent: ... # Agent will respond with "Hello" then "How are you?" ... pass

Source code in autogen/testing/test_agent.py
def __init__(
    self,
    agent: ConversableAgent,
    messages: Iterable[str | dict[str, Any]] = (),
    *,
    suppress_messages_end: bool = False,
) -> None:
    self.agent = agent

    self.__original_human_input = self.agent.human_input_mode

    self.__original_client = agent.client
    self.__fake_client = FakeClient(messages)

    self.suppress_messages_end = suppress_messages_end

agent instance-attribute #

agent = agent

suppress_messages_end instance-attribute #

suppress_messages_end = suppress_messages_end