Skip to main content
In this lesson you’ll see a practical TDD (Test-Driven Development) workflow accelerated by an LLM-based code assistant. We generate a full pytest test suite for a small Python “Unit Converter” utility, run the tests (red), implement the module to satisfy them (green), and handle a real-world precision edge case that required human judgment.
A presentation slide titled "Test-Driven Development with Claude" with a dark teardrop-shaped design on the right containing the word "Demo." The bottom-left corner shows a small "© Copyright KodeKloud" notice.
Why this matters: TDD is powerful when applied consistently. LLM-based assistants speed up the specification and test generation steps, but you still need to run tests, inspect failures, and make precise human decisions for numerical correctness and edge cases. Overview
  • Create a small Python project: a Unit Converter module.
  • Use an LLM-based code assistant to produce a robust pytest suite (test_converter.py).
  • Run tests (expected to fail initially because converter.py is missing).
  • Implement converter.py to satisfy tests.
  • Iterate until all tests pass; investigate any precision failures and decide whether to change constants, test tolerances, or implementation rounding.
Initial project state
  • Virtual environment active (Python 3).
  • File test_converter.py created by an LLM from a detailed prompt (implementation intentionally omitted).
Example prompt used to generate tests (trimmed)
What the generated tests looked like (structure, trimmed)
Running the tests before implementation
  • With only test_converter.py present (no converter.py), pytest fails at collection:
This is the expected TDD “red” phase: tests fail because the implementation is missing. Implementing converter.py Requirements for each function:
  • Validate input is numeric (int or float). If not, raise TypeError.
  • Return the converted value as a float.
  • Prefer well-known constants for conversions to avoid unnecessary precision disagreements.
Reference table of functions and conversion constants Recommended implementation (clean, precise)
Running pytest after implementation
  • After implementing converter.py, run:
  • In the demo workflow this produced 39 passing tests and 1 failure. The failing test revealed a small precision mismatch in the gallons-to-liters expectation:
Precision and the LLM “endless edit” loop
  • LLMs are excellent at producing tests and starter code, but they can trigger small regression cycles: you tweak a constant or rounding to satisfy one test and then break another test. These oscillations require human decisions:
    • Should the implementation use a specific authoritative constant?
    • Is the test tolerance too strict (adjust pytest.approx)?
    • Is rounding required for business logic?
Note: numerical conversions depend on chosen constants and tolerances. Prefer matching documented constants and using pytest.approx() in tests with a reasonable tolerance. Avoid introducing ad-hoc conditional logic in the implementation to satisfy a single test.
Recommendations and practical workflow tips
  • Use an LLM-based assistant to accelerate test generation from a clear, precise prompt.
  • Run tests locally as part of the TDD loop: write (or generate) tests, run them, implement the minimal code to pass, iterate.
  • For floating point comparisons:
    • Use authoritative physical constants where available.
    • Use pytest.approx() with an appropriate tolerance rather than tight equality.
    • If a business requirement dictates strict rounding, document it and implement rounding explicitly.
  • Expect to intervene when the LLM’s automated edits produce regressions—this is part of normal engineering judgment.
Conclusion
  • LLM-based tools are powerful for generating tests and scaffolding implementations, but treat their output as a solid starting point, not a final answer.
  • Follow core TDD steps:
    1. Write/generate tests first.
    2. Run tests and see failures.
    3. Implement minimally to satisfy tests.
    4. Iterate until passing, paying attention to numerical precision and test quality.
  • Human review is essential for precision, edge cases, and architectural choices.
Links and references

Watch Video