Testing
Effective testing and debugging are essential for building reliable applications. This guide covers strategies for testing and debugging complex flows, and monitoring applications in production.
Testing Approaches
Caskada supports multiple testing approaches to ensure your applications work correctly:
Unit Testing (Nodes)
Individual nodes can be tested in isolation to verify their behavior:
import unittest
from unittest.mock import AsyncMock, patch
from caskada import Node
class TestSummarizeNode(unittest.TestCase):
async def test_summarize_node(self):
# Create the node
summarize_node = SummarizeNode()
# Create a mock shared store
memory = {"text": "This is a long text that needs to be summarized."}
# Mock the LLM call
with patch('utils.call_llm', new_callable=AsyncMock) as mock_llm:
mock_llm.return_value = "Short summary."
# Run the node
await summarize_node.run(memory)
# Verify the node called the LLM with the right prompt
mock_llm.assert_called_once()
call_args = mock_llm.call_args[0][0]
self.assertIn("summarize", call_args.lower())
# Verify the result was stored correctly
self.assertEqual(memory.summary, "Short summary.") # Access memory object
if __name__ == "__main__":
# Use asyncio.run for async tests if needed, or run within an existing loop
# For simplicity, assuming standard unittest runner handles async test cases
unittest.main()
Integration Testing (Flows)
Test complete flows to verify that nodes work together correctly:
import unittest
from unittest.mock import AsyncMock, patch
from caskada import Flow
class TestQuestionAnsweringFlow(unittest.TestCase):
async def test_qa_flow(self):
# Create the flow
qa_flow = create_qa_flow()
# Create a mock shared store
memory = {"question": "What is the capital of France?"}
# Mock all LLM calls
with patch('utils.call_llm', new_callable=AsyncMock) as mock_llm:
# Configure the mock to return different values for different prompts
def mock_llm_side_effect(prompt):
if "search" in prompt.lower():
return "Paris is the capital of France."
elif "answer" in prompt.lower():
return "The capital of France is Paris."
return "Unexpected prompt"
mock_llm.side_effect = mock_llm_side_effect
# Run the flow
await qa_flow.run(memory)
# Verify the final answer
self.assertEqual(memory.answer, "The capital of France is Paris.") # Access memory object
# Verify the LLM was called the expected number of times
self.assertEqual(mock_llm.call_count, 2)
if __name__ == '__main__':
# Use asyncio.run for async tests if needed
unittest.main()
Best Practices
Testing Best Practices
Test Each Node Individually: Verify that each node performs its specific task correctly
Test Flows as Integration Tests: Ensure nodes work together as expected
Mock External Dependencies: Use mocks for LLMs, APIs, and databases to ensure consistent testing
Test Error Handling: Explicitly test how your application handles failures
Automate Tests: Include Caskada tests in your CI/CD pipeline
Debugging Best Practices
Start Simple: Begin with a minimal flow and add complexity incrementally
Visualize Your Flow: Generate flow diagrams to understand the structure
Isolate Issues: Test individual nodes to narrow down problems
Check Shared Store: Verify that data is correctly passed between nodes
Monitor Actions: Ensure nodes are returning the expected actions
Monitoring Best Practices
Monitor Node Performance: Track execution time for each node
Watch for Bottlenecks: Identify nodes that take longer than expected
Track Error Rates: Monitor how often nodes and flows fail
Set Up Alerts: Configure alerts for critical failures
Log Judiciously: Log important events without overwhelming storage
Implement Distributed Tracing: Use tracing for complex, distributed applications
By applying these testing techniques, you can ensure your Caskada applications are reliable and maintainable.
Last updated