## टूल्स का सीधे यूनिट टेस्ट करें
टूल्स मूलतः फ़ंक्शंस हैं। इन्हें बिना सर्वर के टेस्ट करें:
def test_add():
assert add(2, 3) == 5
async def test_async_tool():
result = await fetch_user("123")
assert result["id"] == "123"
## कॉन्टेक्स्ट इस्तेमाल करने वाले टूल्स
जो टूल्स get_context() को कॉल करते हैं, उन्हें एक सक्रिय रिक्वेस्ट की आवश्यकता होती है। इन्हें किसी वास्तविक सर्वर के साथ इंटीग्रेशन टेस्ट के ज़रिए चलाएँ, या अपने tool को इस तरह डिज़ाइन करें कि कॉन्टेक्स्ट पर निर्भर हिस्से को mock किया जा सके:
@tool(description="Process with logging")
async def process(data: str) -> dict:
ctx = get_context()
await ctx.info("Processing")
return do_work(data) # do_work को अलग से परीक्षण करें
def test_do_work():
assert do_work("input") == {"result": "output"}
## MCPClient के साथ इंटीग्रेशन टेस्ट
पूर्ण सर्वर का परीक्षण करें:
import pytest
from dedalus_mcp import MCPServer, tool
from dedalus_mcp.client import MCPClient
@tool(description="Add")
def add(a: int, b: int) -> int:
return a + b
@pytest.fixture
async def server():
server = MCPServer("test")
server.collect(add)
# बैकग्राउंड में सर्वर शुरू करें
task = asyncio.create_task(server.serve())
await asyncio.sleep(0.1) # शुरू होने दें
yield server
task.cancel()
@pytest.fixture
async def client(server):
client = await MCPClient.connect("http://127.0.0.1:8000/mcp")
yield client
await client.close()
async def test_call_tool(client):
result = await client.call_tool("add", {"a": 2, "b": 3})
assert result.content[0].text == "5"
## रजिस्ट्रेशन का परीक्षण
जाँचें कि टूल्स सही तरीके से रजिस्टर हो रहे हैं:
def test_registration():
server = MCPServer("test")
server.collect(add, multiply)
names = list(server.tool_names)
assert "add" in names
assert "multiply" in names
## Isolation
क्योंकि decorators इम्पोर्ट के समय servers से bind नहीं होते, हर test को एक clean state मिलती है:
def test_a():
server = MCPServer("test-a")
server.collect(tool_a)
# टियरडाउन की आवश्यकता नहीं
def test_b():
server = MCPServer("test-b")
server.collect(tool_b)
# पूर्णतः स्वतंत्र