RabbitMQ#

Integration with RabbitMQ 4.3 using the official rabbitmq:4.3-management container image.

Installation#

Install pytest-databases and the AMQP client used by your application. The fixture itself does not install or import an AMQP client:

pip install pytest-databases pika

Readiness and the pytest-databases smoke tests use rabbitmq-diagnostics and rabbitmqadmin from inside the container. Both the AMQP port (5672) and the HTTP management port (15672) are mapped.

Usage example#

import pika

from pytest_databases.docker.rabbitmq import RabbitMQService

pytest_plugins = ["pytest_databases.docker.rabbitmq"]


def test_publish(rabbitmq_service: RabbitMQService) -> None:
    connection = pika.BlockingConnection(pika.URLParameters(rabbitmq_service.amqp_url))
    channel = connection.channel()
    channel.queue_declare(queue="example", durable=True)
    channel.basic_publish(exchange="", routing_key="example", body=b"message")
    method, _, body = channel.basic_get(queue="example", auto_ack=True)
    assert method is not None
    assert body == b"message"
    connection.close()

Available fixtures#

  • rabbitmq_image: Container image, defaulting to rabbitmq:4.3-management.

  • rabbitmq_username and rabbitmq_password: Non-guest test credentials.

  • rabbitmq_vhost: Worker-specific virtual host with default xdist isolation.

  • rabbitmq_service: RabbitMQService with host, port, management_port, management_url, container, credentials, virtual host, and amqp_url.

  • xdist_rabbitmq_isolation_level: "database" for one virtual host per worker, or "server" for one transient broker per xdist worker (rabbitmq_<worker>).

Service API#

RabbitMQ AMQP broker container service fixtures and in-container CLI helpers.

class pytest_databases.docker.rabbitmq.RabbitMQService(container: docker.models.containers.Container, host: str, port: int, username: str, password: str, vhost: str, management_port: int = 15672)[source]#

Bases: ServiceContainer

Connection metadata for a RabbitMQ AMQP service.

username: str#
password: str#
vhost: str#
management_port: int = 15672#
property amqp_url: str#

The AMQP connection URL with escaped credentials and virtual host.

property management_url: str#

The HTTP management API base URL.

__init__(container: docker.models.containers.Container, host: str, port: int, username: str, password: str, vhost: str, management_port: int = 15672) → None#
pytest_databases.docker.rabbitmq.provide_rabbitmq_service(container_service: ContainerService, rabbitmq_image: str, rabbitmq_username: str, rabbitmq_password: str, rabbitmq_vhost: str, xdist_rabbitmq_isolation_level: XdistIsolationLevel) → Generator[RabbitMQService, None, None][source]#

Start and yield a ready RabbitMQ AMQP service container.

Yields:

A configured RabbitMQService instance.

pytest_databases.docker.rabbitmq.rabbitmq_image() → str[source]#

Provide the RabbitMQ container image.

pytest_databases.docker.rabbitmq.rabbitmq_password() → str[source]#

Provide the default RabbitMQ password.

pytest_databases.docker.rabbitmq.rabbitmq_service(container_service: ContainerService, rabbitmq_image: str, rabbitmq_username: str, rabbitmq_password: str, rabbitmq_vhost: str, xdist_rabbitmq_isolation_level: XdistIsolationLevel) → Generator[RabbitMQService, None, None][source]#

Start and yield a ready RabbitMQ AMQP service container.

Yields:

A configured RabbitMQService instance.

pytest_databases.docker.rabbitmq.rabbitmq_username() → str[source]#

Provide the default RabbitMQ username.

pytest_databases.docker.rabbitmq.rabbitmq_vhost(xdist_rabbitmq_isolation_level: Literal['database', 'server']) → str[source]#

Provide a worker-scoped RabbitMQ virtual host name.

pytest_databases.docker.rabbitmq.xdist_rabbitmq_isolation_level() → Literal['database', 'server'][source]#

Provide the pytest-xdist isolation level for RabbitMQ.