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:
{% endtab %}
{% tab title="TypeScript (vitest)" %}
{% endtab %} {% endtabs %}
Testing Retry Logic
To test retry behavior:
Simulate Transient Failures: Make the mock function fail a few times before succeeding.
Check Retry Count: Verify that retries happened the expected number of times (e.g., by checking
node.cur_retryinside the mock or tracking calls).Test Backoff: If using
wait, mockasyncio.sleep(Python) orsetTimeout(TypeScript) to verify delays without actually waiting.
{% tabs %} {% tab title="Python (unittest.mock)" %}
{% endtab %}
{% tab title="TypeScript (vitest)" %}
{% endtab %} {% endtabs %}
Test Fixtures and Helpers
Creating helper functions can make tests more readable and maintainable.
{% tabs %} {% tab title="Python (unittest/pytest)" %}
{% endtab %}
{% tab title="TypeScript (vitest)" %}
{% endtab %} {% endtabs %}
Common Testing Patterns
1. Input Validation Testing
Test that nodes properly handle invalid or unexpected inputs.
{% tabs %} {% tab title="Python (pytest)" %}
{% endtab %}
{% tab title="TypeScript (vitest)" %}
{% endtab %} {% endtabs %}
2. Flow Path Testing
Test that flows follow the expected paths based on node triggers.
{% tabs %} {% tab title="Python (unittest/pytest)" %}
{% endtab %}
{% tab title="TypeScript (vitest)" %}
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