Notebook · code/module-09/02-testing-kafka-applications.ipynb

Download 02-testing-kafka-applications.ipynb

Open it in JupyterLab or VS Code from the lab folder to run it. This page is a read-only view.

Testing Kafka applications — practical

Billing's tests live in module-09/tests/, in four layers: the charge calculation alone, the service with fake clients, the transactional flow against a real Kafka in a container, and the schema's compatibility. The notebook runs each layer with pytest, then replays the refactor that under-charged drivers and watches the tests catch it.

Lab services: none of the lab's; the integration test starts its own Kafka container (needs Docker) · Lesson: modules/09-production-ready/02-testing-kafka-applications.md

# Setup: find the lab's shared helpers
import sys, pathlib
lab_root = next(p for p in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents] if (p / "common").is_dir())
sys.path.insert(0, str(lab_root))

import json, os, shutil, subprocess

def pytest(*args, env=None):
    """Run pytest from the code/ folder, as you would in a terminal or in CI, and show its report."""
    print("$ python -m pytest " + " ".join(args))
    result = subprocess.run([sys.executable, "-m", "pytest", *args, "-p", "no:cacheprovider", "--color=no",
                             "--no-header"], cwd=lab_root, capture_output=True, text=True,
                            env={**os.environ, "PY_COLORS": "0", **(env or {})})
    print(result.stdout.strip())

Step 1: Unit tests of the charge

charge_for() is a plain function: no Kafka, no network. Seven hand-picked cases, then all 20,000 scenario sessions at the 0.50 €/kWh tariff, compared with exact decimal arithmetic.

pytest("module-09/tests/test_charge.py", "-v")

Step 2: The service with fake clients

BillingService gets its consumer and producer from outside. Fakes record what it asks of them: the order of the transaction's steps, and what happens when a write fails.

pytest("module-09/tests/test_billing_with_fakes.py", "-v")

Step 3: The transactional flow against a real Kafka

The kafka_bootstrap fixture starts Kafka 4.3.1 in a throwaway container. The test writes 40 sessions, lets billing crash on its 25th charge, restarts it, and reads the charges as an auditor with read_committed.

pytest("module-09/tests/test_billing_integration.py", "-v")

Step 4: Schema compatibility

The tests compare the MeterReading schema in the repo with the published version. Then they try a proposed change: a new field phase_count, first without a default, then with one.

pytest("module-09/tests/test_schema_compatibility.py", "-q")

schema = json.loads((lab_root / "schemas" / "meter_reading.avsc").read_text())
proposals = {"no default": {"name": "phase_count", "type": "int"},
             "default null": {"name": "phase_count", "type": ["null", "int"], "default": None}}
for label, field in proposals.items():
    path = lab_root / ".lab-data" / "proposed-meter-reading.avsc"
    path.parent.mkdir(exist_ok=True)
    path.write_text(json.dumps({**schema, "fields": schema["fields"] + [field]}))
    print(f"\n--- proposed: phase_count with {label}")
    pytest("module-09/tests/test_schema_compatibility.py", "-q", "--tb=line",
           env={"METER_READING_SCHEMA": str(path)})

Break it: the refactor

A refactor replaced the decimal calculation with one line: round(energy * price, 2). Simpler, and right for whole numbers. The notebook applies it to a copy of billing.py and runs the unit tests against the copy.

refactor = lab_root / ".lab-data" / "refactor"
refactor.mkdir(parents=True, exist_ok=True)
code = (lab_root / "module-09" / "billing.py").read_text()
before = '"amount_eur": str(amount.quantize(CENT, rounding=ROUND_HALF_UP))}'
after = '"amount_eur": f"{round(stopped[\'energy_kwh\'] * float(price_eur_per_kwh), 2):.2f}"}'
(refactor / "billing.py").write_text(code.replace(before, after))
print("the refactor:", after)
pytest("module-09/tests/test_charge.py", "-q", "--tb=line", env={"BILLING_DIR": str(refactor)})

Step 5: The whole suite, as CI runs it

Everything, and then only the fast tests, the ones to run on every save.

pytest("module-09/tests", "-q")
pytest("module-09/tests", "-q", "-m", "not integration")

Clean up

shutil.rmtree(refactor, ignore_errors=True)
(lab_root / ".lab-data" / "proposed-meter-reading.avsc").unlink(missing_ok=True)
print("cleaned up")