Redpanda#

Integration with Redpanda, a Kafka-compatible streaming data platform. The fixture starts a single development broker and does not install or import a Python Kafka client.

Installation#

pip install pytest-databases

Install the Kafka client used by your application separately if your tests need one. The service fixture itself remains clientless and uses the image’s rpk command and a standard library Kafka metadata socket probe for startup and readiness checks.

Docker Image#

The default is the pinned official image docker.redpanda.com/redpandadata/redpanda:v26.1.13. The fixture works with both Docker and Podman through the selected container runtime. See Configuration.

Configuration#

  • REDPANDA_IMAGE: image override (default: docker.redpanda.com/redpandadata/redpanda:v26.1.13).

  • REDPANDA_PORT: optional fixed host port. By default the container runtime allocates a free port.

  • xdist_redpanda_isolation_level: xdist isolation level (default: database).

Usage Example#

RedpandaService.bootstrap_servers is the host-reachable Kafka endpoint to pass to your application’s Kafka client. topic_prefix is unique to the xdist worker when logical isolation is active.

The following smoke test stays clientless by running rpk in the service container:

from pytest_databases.docker.redpanda import RedpandaService

pytest_plugins = ["pytest_databases.docker.redpanda"]


def test_redpanda(redpanda_service: RedpandaService) -> None:
    topic = f"{redpanda_service.topic_prefix}events"
    result = redpanda_service.container.exec_run(
        [
            "rpk",
            "topic",
            "create",
            topic,
            "-X",
            "brokers=127.0.0.1:9092",
        ]
    )
    assert result.exit_code == 0
    assert redpanda_service.bootstrap_servers == (
        f"{redpanda_service.host}:{redpanda_service.port}"
    )

Parallel Testing (xdist)#

The default database isolation reuses one broker and gives each worker a topic prefix such as pytest_databases_0_. Tests must apply redpanda_service.topic_prefix to every topic they create.

Override the fixture with "server" when each worker needs a separate broker:

import pytest


@pytest.fixture(scope="session")
def xdist_redpanda_isolation_level():
    return "server"

Server isolation starts one transient container per worker and returns an empty topic prefix.

Available Fixtures#

  • redpanda_image: the Redpanda image reference.

  • redpanda_port: the optional fixed host port.

  • redpanda_service: a running broker with bootstrap_servers and topic_prefix metadata.

  • xdist_redpanda_isolation_level: database or server isolation.

Note

This fixture intentionally exposes Redpanda by name. It does not provide a kafka_service alias or claim complete Apache Kafka compatibility.

Service API#

Redpanda container service fixtures and stdlib Kafka metadata probes.

class pytest_databases.docker.redpanda.RedpandaService(container: docker.models.containers.Container, host: str, port: int, bootstrap_servers: str, topic_prefix: str)[source]#

Bases: ServiceContainer

Metadata for a Kafka-compatible Redpanda broker.

bootstrap_servers: str#
topic_prefix: str#
__init__(container: docker.models.containers.Container, host: str, port: int, bootstrap_servers: str, topic_prefix: str) → None#
pytest_databases.docker.redpanda.fetch_kafka_brokers(host: str, port: int, *, timeout: float = 2.0) → list[tuple[str, int]][source]#

Fetch advertised brokers from a Kafka-compatible listener using a v0 MetadataRequest.

Parameters:
  • host – Broker hostname or IP address.

  • port – Mapped broker TCP port.

  • timeout – Socket timeout in seconds.

Returns:

List of (advertised_host, advertised_port) tuples reported by the broker.

pytest_databases.docker.redpanda.provide_redpanda_service(container_service: ContainerService, redpanda_image: str, redpanda_port: int | None, xdist_redpanda_isolation_level: XdistIsolationLevel) → Generator[RedpandaService, None, None][source]#

Start and manage a clientless Redpanda service with reachable advertised metadata.

Yields:

Service metadata for the running Redpanda broker.

pytest_databases.docker.redpanda.redpanda_image() → str[source]#

Provide the pinned Redpanda image, optionally overridden by the environment.

pytest_databases.docker.redpanda.redpanda_port() → int | None[source]#

Provide an optional host port override for the external Kafka listener.

pytest_databases.docker.redpanda.redpanda_service(container_service: ContainerService, redpanda_image: str, redpanda_port: int | None, xdist_redpanda_isolation_level: XdistIsolationLevel) → Generator[RedpandaService, None, None][source]#

Start a clientless Redpanda service with reachable advertised metadata.

Yields:

Service metadata for the running Redpanda broker.

pytest_databases.docker.redpanda.xdist_redpanda_isolation_level() → Literal['database', 'server'][source]#

Share one broker with worker topic prefixes by default.