> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用标准测试

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

无论是为自己创建自定义类还是发布到 LangChain 集成中，都需要添加测试以确保其按预期工作。LangChain 为每种集成类型提供了一套全面的[测试集](https://pypi.org/project/langchain-tests/)。本指南将向您展示如何将 LangChain 的标准测试套件添加到每种集成类型中。

## 设置

首先，安装所需的依赖项：

<CardGroup cols={2}>
  <Card title="langchain-core" icon="cube" href="https://github.com/langchain-ai/langchain/tree/master/libs/core#readme" arrow>
    定义我们想要导入以定义自定义组件的接口
  </Card>

  <Card title="langchain-tests" icon="flask" href="https://github.com/langchain-ai/langchain/tree/master/libs/standard-tests#readme" arrow>
    提供运行测试所需的标准测试和 `pytest` 插件
  </Card>
</CardGroup>

<Warning>
  由于 `langchain-tests` 新版本中添加的测试可能会破坏您的 CI/CD 流水线，我们建议固定到 [`langchain-tests`](https://pypi.org/project/langchain-tests/#history) 的最新版本，以避免意外更改。
</Warning>

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install -U langchain-core
  pip install -U langchain-tests
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langchain-core
  uv add langchain-tests
  ```
</CodeGroup>

`langchain-tests` 包中有 2 个命名空间：

<AccordionGroup>
  <Accordion title="单元测试" icon="settings">
    **位置**：`langchain_tests.unit_tests`

    设计用于在隔离状态下测试组件，无需访问外部服务

    [查看 API 参考](https://reference.langchain.com/python/langchain_tests/unit_tests)
  </Accordion>

  <Accordion title="集成测试" icon="network">
    **位置**：`langchain_tests.integration_tests`

    设计用于在访问外部服务（特别是组件设计要与之交互的外部服务）的情况下测试组件

    [查看 API 参考](https://reference.langchain.com/python/langchain_tests/integration_tests)
  </Accordion>
</AccordionGroup>

两种类型的测试都实现为基于 [`pytest`](https://docs.pytest.org/en/stable/) 类的测试套件。

## 实现标准测试

根据您的集成类型，您需要实现单元测试和/或集成测试。

通过继承您集成类型的标准测试套件，您将获得该类型的完整标准测试集合。要使测试运行成功，给定测试应仅在模型支持被测试的功能时通过。否则，测试应被跳过。

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

```python tests/integration_tests/test_standard.py theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 指示聊天模型支持图像输入

class TestChatParrotLinkStandard(ChatModelIntegrationTests):
    # ... 其他必需的属性

    @property
    def supports_image_inputs(self) -> bool:
        return True  # （默认值为 False）
```

<Note>
  您应该在相对于包根目录的这些子目录中组织测试：

  * `tests/unit_tests` 用于单元测试
  * `tests/integration_tests` 用于集成测试
</Note>

要查看可配置功能及其默认值的完整列表，请访问标准测试的 [API 参考](https://reference.langchain.com/python/langchain_tests)。

以下是一些来自流行集成的标准测试示例实现：

<Tabs>
  <Tab title="单元测试">
    <Columns cols={3}>
      <Card title="ChatOpenAI" href="https://github.com/langchain-ai/langchain/blob/master/libs/partners/openai/tests/unit_tests/chat_models/test_base_standard.py" arrow>单元测试</Card>
      <Card title="ChatAnthropic" href="https://github.com/langchain-ai/langchain/blob/master/libs/partners/anthropic/tests/unit_tests/test_standard.py" arrow>单元测试</Card>
      <Card title="ChatGenAI" href="https://github.com/langchain-ai/langchain-google/blob/main/libs/genai/tests/unit_tests/test_standard.py" arrow>单元测试</Card>
    </Columns>
  </Tab>

  <Tab title="集成测试">
    <Columns cols={3}>
      <Card title="ChatOpenAI" href="https://github.com/langchain-ai/langchain/blob/master/libs/partners/openai/tests/integration_tests/chat_models/test_base_standard.py" arrow>集成测试</Card>
      <Card title="ChatAnthropic" href="https://github.com/langchain-ai/langchain/blob/master/libs/partners/anthropic/tests/integration_tests/test_standard.py" arrow>集成测试</Card>
      <Card title="ChatGenAI" href="https://github.com/langchain-ai/langchain-google/blob/main/libs/genai/tests/integration_tests/test_standard.py" arrow>集成测试</Card>
    </Columns>
  </Tab>

  <Tab title="沙箱集成测试">
    确保您的集成通过标准测试套件。
    请参阅 [Daytona 集成](https://github.com/langchain-ai/deepagents/blob/main/libs/partners/daytona/tests/integration_tests/test_integration.py) 作为示例。

    <Card title="Daytona" href="https://github.com/langchain-ai/deepagents/blob/main/libs/partners/daytona/tests/integration_tests/test_integration.py" arrow>沙箱集成测试</Card>
  </Tab>
</Tabs>

## 沙箱集成

Deep Agents 沙箱集成使用来自 `langchain_tests.integration_tests` 的 `SandboxIntegrationTests`。
继承它并提供一个 `sandbox` fixture，该 fixture 生成一个 `SandboxBackendProtocol` 实例。
使用 [Daytona 集成测试](https://github.com/langchain-ai/deepagents/blob/main/libs/partners/daytona/tests/integration_tests/test_integration.py) 作为参考实现。
有关发布指南，请参阅[贡献沙箱集成](/oss/python/contributing/integrations-langchain)。

***

## 运行测试

如果从模板引导集成，会提供一个 `Makefile`，其中包含运行单元测试和集成测试的目标：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
make test
make integration_test
```

否则，如果您遵循推荐的目录结构，可以使用以下命令运行测试：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 运行所有测试
uv run --group test pytest tests/unit_tests/
uv run --group test --group test_integration pytest -n auto tests/integration_tests/

# 对于某些单元测试，您可能需要设置某些标志和环境变量：
TIKTOKEN_CACHE_DIR=tiktoken_cache uv run --group test pytest --disable-socket --allow-unix-socket tests/unit_tests/

# 运行特定测试文件
uv run --group test pytest tests/integration_tests/test_chat_models.py

# 运行文件中的特定测试函数
uv run --group test pytest tests/integration_tests/test_chat_models.py::test_chat_completions

# 运行类中的特定测试函数
uv run --group test pytest tests/integration_tests/test_chat_models.py::TestChatParrotLinkIntegration::test_chat_completions
```

## 故障排除

有关可用的标准测试套件的完整列表，以及包含哪些测试和如何排除常见问题的信息，请参阅[标准测试 API 参考](https://reference.langchain.com/python/langchain_tests)。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/contributing/standard-tests-langchain.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
