61 / 89 · 04 API & Contract Testing with AI · Testing Kafka Consumers← prev⊞ allnext →☰ Read as one page
9.7Test Infrastructure: Testcontainers
For isolated Kafka testing, use Testcontainers:
import pytest
from testcontainers.kafka import KafkaContainer
@pytest.fixture(scope="session")
def kafka_container():
"""Start a Kafka container for the test session."""
with KafkaContainer("confluentinc/cp-kafka:7.5.0") as kafka:
yield kafka
@pytest.fixture
def kafka_bootstrap(kafka_container):
return kafka_container.get_bootstrap_server()