I want to test my main agent, the one that is ran ...
# ask-ai
c
I want to test my main agent, the one that is ran with cli.run_app. How can I create unit tests for it?
t
Thanks for reaching out, Anton. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking into creating unit tests for your agent run with
cli.run_app
! A helpful tip is to focus on testing the agent's behavior within an
AgentSession
rather than directly testing
cli.run_app
. Since
cli.run_app
is mainly a high-level entry point for launching the worker and doesn’t contain the core business logic, this approach will give you more meaningful and reliable test results. The LiveKit Agents framework offers some great built-in testing tools to help you evaluate your agent’s behavior effectively. Here’s a friendly guide on how you can proceed: 1. Install Testing Dependencies: It’s a good idea to use
pytest
and
pytest-asyncio
for handling asynchronous tests smoothly:
pip install pytest pytest-asyncio
2. Use
AgentSession
for Behavioral Testing:
Try creating an
AgentSession
and simulate user input with
session.run()
. For example:
Copy code
from livekit.agents import AgentSession
from livekit.plugins import openai
from my_agent import Assistant

@pytest.mark.asyncio
async def test_assistant_greeting():
    async with (
        openai.LLM(model="gpt-4o-mini") as llm,
        AgentSession(llm=llm) as session,
    ):
        await session.start(Assistant())
        result = await session.run(user_input="Hello")
        await result.expect.next_event().is_message(role="assistant").judge(
            llm, intent="Makes a friendly introduction and offers assistance."
        )
        result.expect.no_more_events()
3. Mock Tools for Controlled Testing: You can use the
mock_tools
helper to simulate tool behaviors, which is super helpful for testing how your agent handles errors. 4. Test Multi-Turn Interactions: By making sequential
session.run()
calls, you can test how your agent manages conversation history and handles more complex flows. For more inspiration and full examples, feel free to check out these resources: • Drive-thru agent tests • Front-desk agent tests • Agent starter project Following this approach will help you create reliable and repeatable tests for your agent’s logic without having to depend on the CLI infrastructure. If you have any questions or want to dive deeper, I’m here to help! Sources: livekit-agents/livekit/agents/cli/cli.py | Testing and evaluation | LiveKit Docs | cmd/lk/perf.go
c
That's great, but how do I make sure my AgentSession configuration matches the agent I have in run_app?
t
I'd love to help you out! Could you please share a bit more about how your agent is set up in
cli.run_app
? For instance, are there any specific components, tools, or settings you'd like to make sure are included in the
AgentSession
? Your details will really help me guide you better.