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

设置

首先,安装所需的依赖项:
由于 langchain-tests 新版本中添加的测试可能会破坏您的 CI/CD 管道,我们建议固定到 langchain-tests 的最新版本以避免意外更改。
pip install -U langchain-core
pip install -U langchain-tests
There are 2 namespaces in the langchain-tests package:
Location: langchain_tests.unit_testsDesigned to test the component in isolation and without access to external servicesView API reference
Location: langchain_tests.integration_testsDesigned to test the component with access to external services (in particular, the external service that the component is designed to interact with)View API reference
Both types of tests are implemented as pytest class-based test suites.

Implementing standard tests

Depending on your integration type, you will need to implement either or both unit and integration tests. By subclassing the standard test suite for your integration type, you get the full collection of standard tests for that type. For a test run to be successful, the a given test should pass only if the model supports the capability being tested. Otherwise, the test should be skipped. Because different integrations offer unique sets of features, most standard tests provided by LangChain are opt-in by default to prevent false positives. Consequently, you will need to override properties to indicate which features your integration supports - see the below example for an illustration.
tests/integration_tests/test_standard.py
# Indicate that a chat model supports image inputs

class TestChatParrotLinkStandard(ChatModelIntegrationTests):
    # ... other required properties

    @property
    def supports_image_inputs(self) -> bool:
        return True  # (The default is False)
You should organize tests in these subdirectories relative to the root of your package:
  • tests/unit_tests for unit tests
  • tests/integration_tests for integration tests
To see the complete list of configurable capabilities and their defaults, visit the API reference for standard tests. Here are some example implementations of standard tests from popular integrations:

Running tests

If bootstrapping an integration from a template, a Makefile is provided that includes targets for running unit and integration tests:
make test
make integration_test
Otherwise, if you follow the recommended directory structure, you can run tests with:
# Run all tests
uv run --group test pytest tests/unit_tests/
uv run --group test --group test_integration pytest -n auto tests/integration_tests/

# For certain unit tests, you may need to set certain flags and environment variables:
TIKTOKEN_CACHE_DIR=tiktoken_cache uv run --group test pytest --disable-socket --allow-unix-socket tests/unit_tests/

# Run a specific test file
uv run --group test pytest tests/integration_tests/test_chat_models.py

# Run a specific test function in a file
uv run --group test pytest tests/integration_tests/test_chat_models.py::test_chat_completions

# Run a specific test function within a class
uv run --group test pytest tests/integration_tests/test_chat_models.py::TestChatParrotLinkIntegration::test_chat_completions

Troubleshooting

For a full list of the standard test suites that are available, as well as information on which tests are included and how to troubleshoot common issues, see the Standard Tests API Reference.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.