以编程方式使用文档

标准测试确保您的集成按预期工作。

在为自己创建自定义类或发布LangChain集成时,添加测试以确保其按预期工作是必要的。LangChain提供了全面的 测试集 为每种集成类型提供测试。本指南将向您展示如何为每种集成类型添加LangChain的标准测试套件。

设置

首先,安装所需的依赖项:

langchain-core

定义我们想要导入的接口以定义自定义组件

langchain-tests

提供标准测试和运行它们所需的插件

    npm install @langchain/core
    npm install @langchain/standard-tests
    
    pnpm add @langchain/core
    pnpm add @langchain/standard-tests
    
    yarn add @langchain/core
    yarn add @langchain/standard-tests
    
    bun add @langchain/core
    bun add @langchain/standard-tests
    

中有2个命名空间 langchain-tests package:

Unit tests

位置: src.unit_tests

旨在隔离测试组件,且不访问外部服务

查看API参考

Integration tests

位置: src.integration_tests

旨在测试组件访问外部服务的能力(特别是组件设计要交互的外部服务)

查看API参考

实现标准测试

根据您的集成类型,您需要实现单元测试、集成测试或两者都要实现。

通过为您的集成类型继承标准测试套件,您可以获得该类型的完整标准测试集合。对于测试运行成功,只有在模型支持被测试的能力时,给定测试才应该通过。否则,测试应该被跳过。

由于不同的集成提供独特的功能集,LangChain提供的大多数标准测试 **默认是可选加入的** 以防止误报。因此,您需要覆盖属性以指示您的集成支持哪些功能——请参阅下面的示例进行说明。

// Indicate that a chat model supports parallel tool calls

class ChatParrotLinkStandardIntegrationTests extends ChatModelIntegrationTests<
    ChatParrotLinkCallOptions,
    AIMessageChunk
> {
    constructor() {
        // ... other required properties

        super({
            // ... other required properties
            supportsParallelToolCalls: true,  // (The default is False)
            // ...
        });
    }

要查看可配置功能的完整列表及其默认值,请参阅 实现标准测试.

Sandbox 集成

Deep Agents sandbox 集成使用 sandboxStandardTests@langchain/sandbox-standard-tests. 使用包含以下内容的配置对象调用它 createSandbox, resolvePathcloseSandbox. 使用 Daytona 集成测试 作为参考实现。 请参阅 贡献 sandbox 集成 了解发布指南。

故障排除

有关可用标准测试套件的完整列表,以及有关包含哪些测试以及如何排除常见问题的信息,请参阅 贡献 README.