Event-driven services - Event Contract and Schema validations
This is to try and check how to test event-driven services using Kafka + Testcontainers, JSON Schema validation, and simple contract assertions.
Prerequisites:
- Docker must be running (Testcontainers will start a Kafka broker).
- Python 3.9+ recommended.
Quick start:
python -m venv .venv
.\.venv\Scripts\activate # Windows
pip install -r requirements.txt
pytest -qWhat it contains:
- Sample
schemas/– JSON Schemas forordersandpaymentsevents. src/producer.pyandsrc/consumer.py– helpers usingkafka-python.tests/test_events.py– spins up a Kafka broker with Testcontainers, produces sample events, consumes them and validates withjsonschema.
Docker & Integration Tests
Follow these steps to run the Kafka integration tests locally using Docker.
-
Start Docker Desktop (Windows) or ensure Docker daemon is running.
-
Start Kafka + Zookeeper (uses the repository
docker-compose.yml):
cd C:\SHAN\eda-contract-n-schema-testing
.\scripts\start_kafka.ps1- Run only the fast, no-Docker tests (default):
C:/SHAN/eda-contract-n-schema-testing/.venv/Scripts/python.exe -m pytest -q- Run integration tests that require Docker (marked
integration):
C:/SHAN/eda-contract-n-schema-testing/.venv/Scripts/python.exe -m pytest -q -m integration- Stop Kafka + Zookeeper when finished:
.\scripts\stop_kafka.ps1Notes
- The repo contains three test files:
tests/test_events_nodocker.py— unit/contract checks that run without Docker.tests/test_events.pyandtests/docker_test_events.py— integration tests marked withintegrationand skipped automatically if Docker is unavailable. This is just for learning purpose
- If you run integration tests and
testcontainerstries to start containers but Docker is not running, the tests will be skipped.