Skip to main content

Pytest fixture factories for Zookeeper, Kafka server and Kafka consumer. Read the API docs.

from pathlib import Path
from pytest_kafka import (
    make_zookeeper_process, make_kafka_server, make_kafka_consumer,
    terminate,
)

ROOT = Path(__file__).parent
KAFKA_SCRIPTS = ROOT / 'kafka/bin/'
KAFKA_BIN = str(KAFKA_SCRIPTS / 'kafka-server-start.sh')
ZOOKEEPER_BIN = str(KAFKA_SCRIPTS / 'zookeeper-server-start.sh')

# You can pass a custom teardown function (or parametrise ours). Just don't call it `teardown`
# or Pytest will interpret it as a module-scoped teardown function.
teardown_fn = partial(terminate, signal_fn=Popen.kill)
zookeeper_proc = make_zookeeper_process(ZOOKEEPER_BIN, teardown_fn=teardown_fn)
kafka_server = make_kafka_server(KAFKA_BIN, 'zookeeper_proc', teardown_fn=teardown_fn)
kafka_consumer = make_kafka_consumer(
    'kafka_server', seek_to_beginning=True, kafka_topics=['topic'])

This creates 3 fixtures:

  1. zookeeper_proc - Zookeeper process

  2. kafka_server - Kafka process

  3. kafka_consumer - usable kafka.KafkaConsumer instance

ZOOKEEPER_BIN and KAFKA_BIN are paths to launch scripts in your Kafka distribution. Check this project’s setup.py to see a way of installing Kafka for development.

It is advised to pass seek_to_beginning=True because otherwise some messages may not be captured by the consumer. This requires knowing the topics upfront because without topics there’s no partitions to seek.

Kafka server is known to take a couple of seconds to terminate gracefully. You probably don’t need that, so you can pass partial(terminate, signal_fn=Popen.kill) to make it killed with SIGKILL and waited for afterwards.

It’s possible to create multiple Kafka fixtures forming a cluster by passing the same Zookeeper fixture to them. For an example, check the tests.

Session-scoped fixtures are also available. Consult the test suite.

System requirements

Development

pip install -e .[dev]
./pytest_kafka/install.py  # will install kafka to ./kafka

Acknowledgements

The library has been open-sourced from a codebase belonging to Infectious Media.

Release files for pytest-kafka 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-kafka 0.8.1
File Size Uploaded
pytest_kafka-0.8.1.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-kafka 0.8.1
File Interpreter ABI Platform
pytest_kafka-0.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / pytest_kafka-0.8.1.tar.gz

Download URL pytest_kafka-0.8.1.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c4b709b0e71fe1d1f03608ce38997b32ccfdffea89785a03848d477f2c0248a7
BLAKE2b-256 checksum
How to use checksums
f392d5849b51c0463825da11f9d1ceb5ed0a2f7e94c107c4a8536a4ae88080c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / pytest_kafka-0.8.1-py3-none-any.whl

Download URL pytest_kafka-0.8.1-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0624dc32f60323186438b374b38bde133b66af5fa78bc1aa12dd9ae18d21a522
BLAKE2b-256 checksum
How to use checksums
f1a11284d2ae3681f8c3fbc3a64e7fec471cd0a0117e69f58f1664ac22955909
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

1 release file

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page