> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/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` 夹具，该夹具生成一个 `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="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\contributing\standard-tests-langchain.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
