Skip to main content
在您为 LangGraph 代理创建原型后,一个自然的后续步骤是添加测试。本指南介绍了一些在编写单元测试时可以使用的有用模式。 请注意,本指南是 LangGraph 特定的,涵盖了具有自定义结构的图的场景——如果您刚刚开始,请查看 Test,该指南使用 LangChain 的内置 createAgent 代替。

先决条件

首先,确保您已安装 vitest
$ npm install -D vitest

开始使用

由于许多 LangGraph 代理依赖于状态,一个有用的模式是在每次使用它的测试之前创建您的图,然后在测试中使用新的检查点实例进行编译。 下面的示例展示了一个简单的线性图如何通过 node1node2 进展。每个节点更新单个状态键 my_key
import { test, expect } from 'vitest';
import {
  StateGraph,
  StateSchema,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('basic agent execution', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  const result = await compiledGraph.invoke(
    { my_key: 'initial_value' },
    { configurable: { thread_id: '1' } }
  );
  expect(result.my_key).toBe('hello from node2');
});

测试单个节点和边

编译后的 LangGraph 代理通过 graph.nodes 公开对每个单独节点的引用。您可以利用这一点来测试代理中的单个节点。请注意,这将绕过编译图时传递的任何检查点器:
import { test, expect } from 'vitest';
import {
  StateGraph,
  START,
  END,
  MemorySaver,
  StateSchema,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('individual node execution', async () => {
  const uncompiledGraph = createGraph();
  // 在此示例中将被忽略
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  // 仅调用节点 1
  const result = await compiledGraph.nodes['node1'].invoke(
    { my_key: 'initial_value' },
  );
  expect(result.my_key).toBe('hello from node1');
});

部分执行

对于由较大图组成的代理,您可能希望测试代理内的部分执行路径,而不是整个端到端流程。在某些情况下,将这些部分重构为子图可能具有语义意义,您可以像正常情况一样单独调用它们。 但是,如果您不希望更改代理图的整体结构,可以使用 LangGraph 的持久化机制来模拟代理在所需部分开始之前暂停的状态,并在所需部分结束时再次暂停。步骤如下:
  1. 使用检查点器编译您的代理(内存检查点器 MemorySaver 对于测试来说就足够了)。
  2. 使用 asNode 参数调用代理的 update_state 方法,该参数设置为您想要开始测试的节点之前的节点名称。
  3. 使用与更新状态时相同的 thread_id 调用您的代理,并将 interruptBefore 参数设置为您想要停止的节点名称。
以下示例仅执行线性图中的第二个和第三个节点:
import { test, expect } from 'vitest';
import {
  StateGraph,
  StateSchema,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addNode('node3', (state) => ({ my_key: 'hello from node3' }))
    .addNode('node4', (state) => ({ my_key: 'hello from node4' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', 'node3')
    .addEdge('node3', 'node4')
    .addEdge('node4', END);
};

test('partial execution from node2 to node3', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  await compiledGraph.updateState(
    { configurable: { thread_id: '1' } },
    // 传递给节点 2 的状态 - 模拟节点 1 结束时的状态
    { my_key: 'initial_value' },
    // 更新保存的状态,就好像它来自节点 1
    // 执行将从节点 2 恢复
    'node1',
  );
  const result = await compiledGraph.invoke(
    // 通过传递 None 恢复执行
    null,
    {
      configurable: { thread_id: '1' },
      // 在节点 3 之后停止,以便节点 4 不运行
      interruptAfter: ['node3']
    },
  );
  expect(result.my_key).toBe('hello from node3');
});