Kafka Module¶
The Kafka module provides steps for testing Kafka producers and consumers.
It covers bootstrap server configuration, message production (single, JSON,
batch), message consumption (with optional timeout), assertions on consumed
messages, store/extract operations and advanced producer/consumer
configuration — 20 steps in total, all with es and pt translations.
Installation¶
pip install "behave-steplib[kafka]"
Backends¶
Backend |
Package |
Notes |
|---|---|---|
|
|
Kafka producer and consumer. Requires the |
Steps¶
Configuration¶
Pattern |
Description |
|---|---|
|
Set the Kafka bootstrap servers. |
|
Set the Kafka consumer group ID. |
|
Set the auto offset reset strategy ( |
|
Set additional Kafka producer configuration overrides from JSON. |
|
Set additional Kafka consumer configuration overrides from JSON. |
Produce¶
Pattern |
Description |
|---|---|
|
Produce a message to a Kafka topic. |
|
Produce a JSON-serialized message to a Kafka topic. |
|
Produce multiple messages to a Kafka topic from a behave table. |
Consume¶
Pattern |
Description |
|---|---|
|
Consume messages from a Kafka topic and store them in
|
|
Consume messages from a Kafka topic with a custom timeout. |
Assertions¶
Pattern |
Description |
|---|---|
|
Assert the number of consumed messages. |
|
Assert the number of consumed messages is greater than a value. |
|
Assert at least one consumed message value contains text. |
|
Assert the key of the message at a given index equals a value. |
|
Assert the value of the message at a given index equals a value. |
|
Assert at least one message value matches a regex pattern. |
|
Assert message keys appear in a specific comma-separated order. |
Store and extract¶
Pattern |
Description |
|---|---|
|
Store the value of a consumed message as a variable. |
|
Store the key of a consumed message as a variable. |
|
Store the number of consumed messages as a variable. |
Example¶
Feature: Kafka messaging
Scenario: Produce and consume a message
Given the Kafka bootstrap servers are "localhost:9092"
When I produce a message to topic "events" with key "id" and value "hello"
And I consume messages from topic "events"
Then the consumed messages count is 1
And a consumed message contains "hello"
Producer and consumer lifecycle¶
The KafkaContext holds the producer
and consumer. They are created lazily on first use and closed by
cleanup():
def before_all(context):
context.steplib = autoload(context)
def after_scenario(context, scenario):
context.steplib.cleanup() # closes producer and consumer
API reference¶
See Modules API Reference for the full autodoc reference of the Kafka module.