---
question: How do I test an integration without the real API?
kind: task
via_web: na
via_cli: available
via_python: available
via_api: na
via_config: na
ref_cli: uv run pytest
ref_python: unittest.mock.AsyncMock
prereq: You need an integration class with its HTTP call in one method. See [Create an integration](/capabilities/build/agents/creating-an-integration).
next:
  - /capabilities/build/integrations/use-in-an-agent
  - /capabilities/build/modules/test-a-module
---

# Test an integration

Test an integration as plain Python: build the class with a fake key, replace its HTTP call, and check what it returns. No engine, no broker and no network.

Keep the test next to the class, as `<Name>Integration_test.py`: `abi new integration` writes one, with two tests of the scaffold's `ping()`. Keep tests that call the real service in a separate `<Name>Integration_integration_test.py`, with a skip when the credential is missing: see [Skip live tests without credentials](#skip-live-tests-without-credentials).

## Web UI

Not applicable. Nexus has no screen that runs an integration or its tests. Run them with the CLI. To try an integration through an agent, see [Use in an agent](/capabilities/build/integrations/use-in-an-agent).

## CLI

### Run the module's integration tests

From the project root:

```bash
uv run pytest src/my_module/integrations
```

pytest collects every `*_test.py` file in the folder.

### Run one file

```bash
uv run pytest src/my_module/integrations/MyIntegration_test.py -v
```

There is no `abi` command that tests an integration.

## Python

### Write the test

The integration's methods are `async`. Patch the method the class uses for HTTP with an `AsyncMock`, and run the call with `asyncio.run`, as the scaffolded test does. In the example on [Create an integration](/capabilities/build/agents/creating-an-integration), that method is `_make_request`:

```python title="integrations/MyIntegration_test.py"
import asyncio
from unittest.mock import AsyncMock, patch

from .MyIntegration import MyIntegration, MyIntegrationConfiguration


def test_list_items():
    integration = MyIntegration(MyIntegrationConfiguration(api_key="test-key"))
    with patch.object(
        integration, "_make_request", AsyncMock(return_value=[{"id": "1"}])
    ) as request:
        result = asyncio.run(integration.list_items(filter="open"))
    assert result == [{"id": "1"}]
    request.assert_awaited_once_with("/items", params={"filter": "open"})
```

### Test a missing key

The scaffold's `ping()` raises `IntegrationConnectionError` when the configuration has no key. The test `abi new integration` wrote checks it:

```python title="integrations/MyIntegration_test.py"
import pytest
from naas_abi_sdk.integration import IntegrationConnectionError


def test_refuses_to_run_without_credentials():
    integration = MyIntegration(MyIntegrationConfiguration())
    with pytest.raises(IntegrationConnectionError):
        asyncio.run(integration.ping())
```

### Test the tools

`as_tools(configuration)` returns LangChain tools. Call one with `ainvoke` and its arguments to check the schema and the wiring. In the same test file:

```python title="integrations/MyIntegration_test.py"
from .MyIntegration import as_tools


def test_tools():
    tools = {tool.name: tool for tool in as_tools(MyIntegrationConfiguration(api_key="test-key"))}
    with patch.object(MyIntegration, "_make_request", AsyncMock(return_value=[])):
        assert asyncio.run(tools["list_myservice_items"].ainvoke({"filter": "open"})) == []
```

## REST API

Not applicable. No API has a route for integrations: nothing calls or tests them over HTTP. Test with the CLI or Python.

## Config

Not applicable. Tests build the configuration in code with a fake key. They do not read `.env` or the secret service.

## Skip live tests without credentials

`<Name>Integration_integration_test.py` still ends in `_test.py`, so `uv run pytest` collects it with the others. Skip it when the key is not set, so a run without credentials passes:

```python title="integrations/MyIntegration_integration_test.py"
import asyncio
import os

import pytest

from .MyIntegration import MyIntegration, MyIntegrationConfiguration

pytestmark = pytest.mark.skipif(
    not os.getenv("MY_API_KEY"), reason="MY_API_KEY is not set"
)


def test_list_items_live():
    integration = MyIntegration(MyIntegrationConfiguration(api_key=os.environ["MY_API_KEY"]))
    assert isinstance(asyncio.run(integration.list_items()), list)
```

Export `MY_API_KEY` in the shell to run them against the real service. A test reads the key from its own environment; the module itself reads it from the secret service.
