RabbitMQ
qa_pytest_rabbitmq
__all__ = ['Message', 'QueueHandler', 'RabbitMqConfiguration', 'RabbitMqSteps', 'RabbitMqTests']
module-attribute
Message
dataclass
Represents a message to be published or consumed from a RabbitMQ queue.
Attributes:
Name | Type | Description |
---|---|---|
content |
V
|
The message payload. |
properties |
BasicProperties
|
Optional message properties for RabbitMQ. |
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
16 17 18 19 20 21 22 23 24 25 26 27 |
|
content
instance-attribute
properties = field(default_factory=BasicProperties)
class-attribute
instance-attribute
__init__(content, properties=BasicProperties())
QueueHandler
dataclass
Bases: LoggerMixin
Handles publishing and consuming messages from a RabbitMQ queue in a thread-safe, asynchronous manner.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel
|
BlockingChannel
|
The RabbitMQ channel to use. |
required |
queue_name
|
str
|
The name of the queue to operate on. |
required |
indexing_by
|
Callable
|
Function to extract a key from a message. |
required |
consuming_by
|
Callable
|
Function to deserialize message bytes. |
required |
publishing_by
|
Callable
|
Function to serialize message content. |
required |
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
channel
instance-attribute
consuming_by
instance-attribute
indexing_by
instance-attribute
publishing_by
instance-attribute
queue_name
instance-attribute
received_messages
property
Returns a snapshot of all received messages, indexed by key.
Returns:
Type | Description |
---|---|
Mapping[K, Message[V]]
|
Mapping[K, Message[V]]: The received messages. |
__enter__()
Context manager entry. Returns self.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
69 70 71 72 73 |
|
__exit__(exc_type, exc_value, traceback)
Context manager exit. Ensures the handler is closed and resources are released.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
75 76 77 78 79 80 81 82 83 84 |
|
__init__(channel, queue_name, indexing_by, consuming_by, publishing_by, _received_messages=lambda: dict()(), _command_queue=lambda: queue.Queue()())
__post_init__()
Starts the worker thread for handling asynchronous queue operations.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
61 62 63 64 65 66 67 |
|
cancel()
Cancels the active consumer, if any. Returns: str: The previous consumer tag, or an empty string if none.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
close()
Gracefully shuts down the handler, cancels consumers, and joins the worker thread.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
184 185 186 187 188 189 190 |
|
consume()
Starts consuming messages from the queue asynchronously. Returns: str: A placeholder consumer tag (actual tag is set internally).
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
publish(messages)
Publishes an iterable of Message objects to the queue asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
Iterator[Message[V]]
|
The messages to publish. |
required |
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
publish_values(values)
Publishes an iterable of values to the queue, wrapping each in a Message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Iterator[V]
|
The values to publish. |
required |
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/queue_handler.py
175 176 177 178 179 180 181 182 |
|
RabbitMqConfiguration
Bases: BaseConfiguration
RabbitMQ-specific test configuration. Provides access to the RabbitMQ connection URI from the configuration parser.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_configuration.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
connection_uri
cached
property
Returns the RabbitMQ connection URI as a pika.URLParameters object.
Returns:
Type | Description |
---|---|
URLParameters
|
pika.URLParameters: The connection URI for RabbitMQ. |
RabbitMqSteps
Bases: GenericSteps[TConfiguration]
BDD-style step definitions for RabbitMQ queue operations.
Type Parameters
K: The type of the message key. V: The type of the message content. TConfiguration: The configuration type, must be a RabbitMqConfiguration.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_steps.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
a_queue_handler(queue_handler)
Sets the queue handler to use for subsequent steps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
queue_handler
|
QueueHandler[K, V]
|
The handler instance. |
required |
Returns: Self: The current step instance for chaining.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_steps.py
26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
consuming()
Starts consuming messages from the queue.
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
The current step instance for chaining. |
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_steps.py
54 55 56 57 58 59 60 61 62 63 64 |
|
publishing(messages)
Publishes the provided messages to the queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
Iterable[Message[V]]
|
The messages to publish. |
required |
Returns: Self: The current step instance for chaining.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_steps.py
40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
the_message_by_key(key, by_rule)
Asserts that the message with the given key matches the provided matcher rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
K
|
The key to look up. |
required |
by_rule
|
Matcher[Message[V]]
|
Matcher for the message. |
required |
Returns: Self: The current step instance for chaining.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_steps.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
the_received_messages(by_rule)
Asserts that the received messages match the provided matcher rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by_rule
|
Matcher[Iterator[Message[V]]]
|
Matcher for the received messages iterator. |
required |
Returns: Self: The current step instance for chaining.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_steps.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
RabbitMqTests
Bases: AbstractTestsBase[TSteps, TConfiguration]
Base class for BDD-style RabbitMQ integration tests. Manages the lifecycle of a RabbitMQ connection for test scenarios.
Type Args
K: The type of the message key. V: The type of the message content. TSteps: The steps implementation type. TConfiguration: The configuration type, must be a RabbitMqConfiguration.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_tests.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
setup_method()
Sets up the RabbitMQ connection before each test method.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_tests.py
29 30 31 32 33 34 35 36 |
|
teardown_method()
Tears down the RabbitMQ connection after each test method.
Source code in qa-pytest-rabbitmq/src/qa_pytest_rabbitmq/rabbitmq_tests.py
38 39 40 41 42 43 44 45 46 47 |
|