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

# 集成测试

> 通过组织测试、管理密钥、处理不稳定性和控制成本，使用真实 LLM API 测试智能体。

集成测试验证你的智能体是否能与模型 API 和外部服务正确协作。与使用虚拟对象和模拟的[单元测试](/oss/python/langchain/test/unit-testing)不同，集成测试会进行实际的网络调用，以确认组件能够协同工作、凭据有效且延迟可接受。

由于大语言模型(LLM)的响应是非确定性的，集成测试需要与传统软件测试不同的策略。本指南介绍如何组织、编写和运行智能体的集成测试。关于为 LangChain 本身贡献代码时的通用测试基础设施，请参阅[贡献代码](/oss/python/contributing/code#running-tests)。

## 分离单元测试和集成测试

集成测试速度较慢且需要 API 凭据，因此请将它们与单元测试分开。这样你可以在每次更改时运行快速的单元测试，而将集成测试留给 CI 或部署前检查。

使用 pytest 标记来标注集成测试：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import pytest

@pytest.mark.integration
def test_agent_with_real_model():
    agent = create_agent("claude-sonnet-4-6", tools=[get_weather])
    result = agent.invoke({
        "messages": [HumanMessage(content="What's the weather in SF?")]
    })
    assert len(result["messages"]) > 1
```

配置 pytest 以识别标记并从默认运行中排除集成测试：

<CodeGroup>
  ```ini pytest.ini theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  [pytest]
  markers =
      integration: tests that call real LLM APIs
  addopts = -m "not integration"
  ```

  ```toml pyproject.toml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  [tool.pytest.ini_options]
  markers = [
    "integration: tests that call real LLM APIs"
  ]
  addopts = "-m 'not integration'"
  ```
</CodeGroup>

显式运行集成测试：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pytest -m integration
```

## 管理 API 密钥

集成测试需要真实的 API 凭据。从环境变量加载它们，这样密钥就不会进入源代码管理。

使用 `conftest.py` fixture 验证所需的密钥是否可用：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os
import pytest

@pytest.fixture(autouse=True)
def check_api_keys():
    if not os.environ.get("OPENAI_API_KEY"):
        pytest.skip("OPENAI_API_KEY not set")
```

对于本地开发，将密钥存储在 `.env` 文件中，并使用 [`python-dotenv`](https://pypi.org/project/python-dotenv/) 加载：

```bash .env theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
OPENAI_API_KEY=sk-...
```

```python conftest.py theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from dotenv import load_dotenv

load_dotenv()
```

<Warning>
  将 `.env` 添加到你的 `.gitignore` 中以避免提交凭据。在 CI 中，通过你的提供商的密钥管理（例如 GitHub Actions secrets）注入密钥。
</Warning>

## 断言结构而非内容

大语言模型(LLM)的响应在不同运行之间会有所不同。不要断言精确的输出字符串，而是验证响应的结构属性：消息类型、工具调用名称、参数格式和消息数量。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
def test_agent_calls_weather_tool():
    agent = create_agent("claude-sonnet-4-6", tools=[get_weather])
    result = agent.invoke({
        "messages": [HumanMessage(content="What's the weather in SF?")]
    })

    messages = result["messages"]
    tool_calls = [
        tc
        for msg in messages
        if hasattr(msg, "tool_calls")
        for tc in (msg.tool_calls or [])
    ]

    assert any(tc["name"] == "get_weather" for tc in tool_calls)
    assert isinstance(messages[-1], AIMessage)
    assert len(messages[-1].content) > 0
```

<Tip>
  要进行更严格的轨迹断言，请使用 [AgentEvals](/oss/python/langchain/test/evals) 评估器，它支持 `unordered` 和 `superset` 等模糊匹配模式。
</Tip>

## 降低成本和延迟

调用 LLM API 的集成测试会产生实际成本。一些实践有助于保持测试套件快速且经济：

* **使用较小的模型**：`gemini-3.1-flash-lite-preview` 或同等级别的模型，用于只需要验证工具调用和响应结构的测试。
* **设置 `maxTokens`**：限制响应长度以避免长时间、昂贵的补全。
* **限制测试范围**：每个测试只测试一种行为。当单轮测试就足够时，避免链接多次 LLM 调用的端到端场景。
* **选择性运行**：使用[上述](#分离单元测试和集成测试)的测试分离方案，仅在 CI 或部署前运行集成测试，而不是每次保存文件时都运行。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
agent = create_agent(
    "gemini-3.1-flash-lite-preview",
    tools=[get_weather],
    model_kwargs={"max_tokens": 256},
)
```

## 录制和回放 HTTP 调用

对于在 CI 中频繁运行的测试，你可以在第一次运行时录制 HTTP 交互，并在后续运行中回放，无需进行真实 API 调用。这在初始录制后消除了成本和延迟。

[`vcrpy`](https://pypi.org/project/vcrpy/1.5.2/) 将 HTTP 请求/响应对录制到 YAML "磁带"文件中。[`pytest-recording`](https://pypi.org/project/pytest-recording/) 插件将其与 pytest 集成。

设置你的 `conftest.py` 以从磁带中过滤敏感信息：

```py conftest.py theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import pytest

@pytest.fixture(scope="session")
def vcr_config():
    return {
        "filter_headers": [
            ("authorization", "XXXX"),
            ("x-api-key", "XXXX"),
        ],
        "filter_query_parameters": [
            ("api_key", "XXXX"),
            ("key", "XXXX"),
        ],
    }
```

配置你的项目以识别 `vcr` 标记：

<CodeGroup>
  ```ini pytest.ini theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  [pytest]
  markers =
      vcr: record/replay HTTP via VCR
  addopts = --record-mode=once
  ```

  ```toml pyproject.toml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  [tool.pytest.ini_options]
  markers = [
    "vcr: record/replay HTTP via VCR"
  ]
  addopts = "--record-mode=once"
  ```
</CodeGroup>

<Info>
  `--record-mode=once` 选项在第一次运行时录制 HTTP 交互，在后续运行中回放它们。
</Info>

用 `vcr` 标记装饰你的测试：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
@pytest.mark.vcr()
def test_agent_trajectory():
    agent = create_agent("claude-sonnet-4-6", tools=[get_weather])
    result = agent.invoke({
        "messages": [HumanMessage(content="What's the weather in SF?")]
    })
    assert any(
        tc["name"] == "get_weather"
        for msg in result["messages"]
        if hasattr(msg, "tool_calls")
        for tc in (msg.tool_calls or [])
    )
```

第一次运行会进行真实网络调用并在 `tests/cassettes/` 中生成磁带文件。后续运行会回放录制的响应。

<Warning>
  当你修改提示词、添加新工具或更改预期轨迹时，你保存的磁带将过期，现有测试**将会失败**。删除相应的磁带文件并重新运行测试以录制新的交互。
</Warning>

## 后续步骤

了解如何使用确定性匹配或 LLM 作为评判者的评估器来评估智能体轨迹，请参阅[评估](/oss/python/langchain/test/evals)。

***

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

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