主题
测试
在完成 LangGraph 智能体的原型设计之后,自然的下一步就是添加测试。本指南涵盖了一些在编写单元测试时可以使用的实用模式。
请注意,本指南特定于 LangGraph,涵盖围绕自定义结构图的场景——如果你刚刚开始,请查看 Test,它改为使用 LangChain 内置的 create_agent。
前置条件
首先,确保你已经安装了 pytest:
bash
$ pip install -U pytest入门
由于许多 LangGraph 智能体依赖状态,一个实用的模式是在每次使用图的测试之前创建该图,然后在测试内使用新的检查点器实例来编译它。
下面的示例展示了它如何与一个简单的线性图配合工作,该图依次经过 node1 和 node2。每个节点都更新单个状态键 my_key:
python
import pytest
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import MemorySaver
def create_graph() -> StateGraph:
class MyState(TypedDict):
my_key: str
graph = StateGraph(MyState)
graph.add_node("node1", lambda state: {"my_key": "hello from node1"})
graph.add_node("node2", lambda state: {"my_key": "hello from node2"})
graph.add_edge(START, "node1")
graph.add_edge("node1", "node2")
graph.add_edge("node2", END)
return graph
def test_basic_agent_execution() -> None:
checkpointer = MemorySaver()
graph = create_graph()
compiled_graph = graph.compile(checkpointer=checkpointer)
result = compiled_graph.invoke(
{"my_key": "initial_value"},
config={"configurable": {"thread_id": "1"}}
)
assert result["my_key"] == "hello from node2"测试单个节点和边
编译后的 LangGraph 智能体会以 graph.nodes 暴露对每个单独节点的引用。你可以利用这一点来测试智能体内的单个节点。请注意,这将绕过编译图时传入的任何检查点器:
python
import pytest
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import MemorySaver
def create_graph() -> StateGraph:
class MyState(TypedDict):
my_key: str
graph = StateGraph(MyState)
graph.add_node("node1", lambda state: {"my_key": "hello from node1"})
graph.add_node("node2", lambda state: {"my_key": "hello from node2"})
graph.add_edge(START, "node1")
graph.add_edge("node1", "node2")
graph.add_edge("node2", END)
return graph
def test_individual_node_execution() -> None:
# Will be ignored in this example
checkpointer = MemorySaver()
graph = create_graph()
compiled_graph = graph.compile(checkpointer=checkpointer)
# Only invoke node 1
result = compiled_graph.nodes["node1"].invoke(
{"my_key": "initial_value"},
)
assert result["my_key"] == "hello from node1"部分执行
对于由较大的图构成的智能体,你可能希望测试智能体内的部分执行路径,而不是对整个流程进行端到端测试。在某些情况下,将这些部分重构为子图在语义上可能更合理,你可以像平常一样单独调用它们。
但是,如果你不希望更改智能体图的整体结构,可以使用 LangGraph 的持久化机制来模拟一种状态:让智能体恰好在所需部分开始之前暂停,并在所需部分结束时再次暂停。步骤如下:
- 使用检查点器编译你的智能体(用于测试时,内存检查点器
InMemorySaver就足够了)。 - 调用智能体的
update_state方法,并将as_node参数设置为你希望开始测试的那个节点之前的节点名称。 - 使用与更新状态时相同的
thread_id调用你的智能体,并将interrupt_after参数设置为你希望停止的节点名称。
下面是一个只执行线性图中第二个和第三个节点的示例:
python
import pytest
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import MemorySaver
def create_graph() -> StateGraph:
class MyState(TypedDict):
my_key: str
graph = StateGraph(MyState)
graph.add_node("node1", lambda state: {"my_key": "hello from node1"})
graph.add_node("node2", lambda state: {"my_key": "hello from node2"})
graph.add_node("node3", lambda state: {"my_key": "hello from node3"})
graph.add_node("node4", lambda state: {"my_key": "hello from node4"})
graph.add_edge(START, "node1")
graph.add_edge("node1", "node2")
graph.add_edge("node2", "node3")
graph.add_edge("node3", "node4")
graph.add_edge("node4", END)
return graph
def test_partial_execution_from_node2_to_node3() -> None:
checkpointer = MemorySaver()
graph = create_graph()
compiled_graph = graph.compile(checkpointer=checkpointer)
compiled_graph.update_state(
config={
"configurable": {
"thread_id": "1"
}
},
# The state passed into node 2 - simulating the state at
# the end of node 1
values={"my_key": "initial_value"},
# Update saved state as if it came from node 1
# Execution will resume at node 2
as_node="node1",
)
result = compiled_graph.invoke(
# Resume execution by passing None
None,
config={"configurable": {"thread_id": "1"}},
# Stop after node 3 so that node 4 doesn't run
interrupt_after="node3",
)
assert result["my_key"] == "hello from node3"