how to write unit testing? give me code example
# ask-ai
a
how to write unit testing? give me code example
t
Thanks for reaching out, affandy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you with a unit testing example! Could you please let me know which programming language or framework you're working with? That way, I can give you the most relevant and useful guidance.
a
python
t
I'd be happy to help you get started with writing unit tests in Python! You can use the built-in
unittest
module or the popular third-party
pytest
framework. Here's a simple example using
unittest
to guide you:
Copy code
import unittest

class Calculator:
    def add(self, a, b):
        return a + b

    def divide(self, a, b):
        if b == 0:
            raise ValueError("Cannot divide by zero")
        return a / b

class TestCalculator(unittest.TestCase):
    def setUp(self):
        self.calculator = Calculator()

    def test_add(self):
        self.assertEqual(self.calculator.add(2, 3), 5)

    def test_divide(self):
        self.assertEqual(self.calculator.divide(10, 2), 5.0)

    def test_divide_by_zero(self):
        with self.assertRaises(ValueError):
            self.calculator.divide(10, 0)

if __name__ == '__main__':
    unittest.main()
If you prefer something a bit more flexible and concise,
pytest
is a wonderful option. Here's how you could write similar tests with it:
Copy code
import pytest

class Calculator:
    def add(self, a, b):
        return a + b

    def divide(self, a, b):
        if b == 0:
            raise ValueError("Cannot divide by zero")
        return a / b

def test_add():
    calculator = Calculator()
    assert calculator.add(2, 3) == 5

def test_divide():
    calculator = Calculator()
    assert calculator.divide(10, 2) == 5.0

def test_divide_by_zero():
    calculator = Calculator()
    with pytest.raises(ValueError):
        calculator.divide(10, 0)
Please feel free to reach out if you'd like me to walk you through more examples or help with anything else. I'm here to support you! Sources: Testing and evaluation | LiveKit Docs | complex-agents/testing/agent_test.py | .github/workflows/tests.yml
a
I mean, I want to do unit test on livekit agent codes
t
To help you write unit tests for your LiveKit agent code, I recommend using the
pytest
framework along with
pytest-asyncio
. Here’s a simple example to get you started with testing an agent's behavior:
Copy code
import pytest
from livekit.agents import MockedLLM, TestAgentSession
from your_agent_module import entrypoint  # Replace with your agent's entrypoint

@pytest.mark.asyncio
async def test_greeting():
    session = TestAgentSession(entrypoint_fnc=entrypoint)
    events = await session.say("Hello")

    # Assert the agent responded with a greeting
    assert any("hello" in evt.text.lower() or "hi " in evt.text.lower() 
               for evt in events if evt.type == "agent_speech")
Here are a few helpful tips for testing your LiveKit agents: • Use
TestAgentSession
to simulate conversations with your agent. • Mock external services such as LLMs with
MockedLLM
to make your tests faster and more reliable. • Try testing multi-turn interactions by chaining
session.say()
calls to see how your agent handles ongoing conversations. • Check tool usage and error handling by using
mock_tools
to cover those scenarios. If you’d like, I’m here to provide more examples or walk you through any part of this. Just let me know! Sources: Testing and evaluation | LiveKit Docs | tests/__init__.py | complex-agents/drive-thru/test_agent.py | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik...