Skip to main content
This lesson walks through a practical test-driven development (TDD) example using Claude Code to generate tests and iterate on an implementation. The sample project is a compact unit-converter supporting distance, volume, mass, and temperature conversions. TDD workflow recap:
  • Write tests first.
  • Run tests and watch them fail (red).
  • Implement the smallest amount of code to make the tests pass (green).
  • Refactor while keeping tests green.
When a team consistently applies TDD it reduces regressions and improves long-term velocity. Claude Code can accelerate the mechanical, repetitive parts of TDD such as generating comprehensive test cases.

Requirements and test expectations

  • Every conversion function accepts a single numeric argument (int or float) and returns a float.
  • Note: in Python, bool is a subclass of int and will satisfy isinstance(value, (int, float)). If you need to reject booleans (True/False), adjust the input validation accordingly.
  • If a non-numeric value is passed, the functions must raise TypeError.
  • Tests should cover: integers, floats, zero, negative values, and invalid inputs.
  • Use pytest.approx() in tests where floating-point precision matters.

Conversion formulas and constants

Below is a concise table of the functions, formulas, and precise conversion factors used by the tests and implementation.

Using Claude Code to generate tests

Workflow used:
  1. Craft a clear prompt describing required test coverage for converter.py.
  2. Ask Claude Code to generate a comprehensive pytest suite (test_converter.py).
  3. Run pytest to see failing tests (expected first red).
  4. Implement converter.py to satisfy tests, iterate until green.
The generated test suite covers:
  • Correctness for integers and floats.
  • Zero input scenarios.
  • Negative value tests.
  • TypeError tests for non-numeric inputs.
  • Use of pytest.approx() for floating-point comparisons.
Example test file header and organization (representative):
The test file was edited in VS Code; when saving the test file the editor prompted to write changes:
A screenshot of Visual Studio Code with a text file open containing instructions to create pytest tests for a Python unit-converter (test_converter.py). The integrated terminal shows a prompt asking whether to save the edits to test_converter.py.

Running the tests (first run)

Before implementing converter.py, running pytest fails at collection:
This is the expected “red” step in the red/green/refactor cycle. Now implement the module to satisfy the tests.

Implementing converter.py

Implementation goals:
  • Validate inputs and raise TypeError for non-numeric values.
  • Use correct formulas and precise conversion constants.
  • Always return a float.
A concise and correct implementation:

Iterating with pytest

  • Re-run pytest after implementing the module and fix any failures.
  • Floating-point mismatches can occur between constants used in tests and implementation. Tests should use pytest.approx() to tolerate reasonable differences.
  • If a persistent mismatch remains, prefer improving implementation precision rather than loosening tests unless the tests are incorrect.
Example successful test run:
LLMs (such as Claude Code) are powerful for generating comprehensive, mechanical test suites and scaffolding implementations. Always perform human review and iterative testing—LLM outputs are excellent starting points but may need adjustments for precision, edge cases, and integration context.

Summary and recommendations

  • TDD gives you a precise specification and reduces regressions.
  • Use Claude Code to speed up repetitive tasks like generating extensive test cases.
  • Always run and iterate on tests locally: LLMs may need guidance around precision and edge cases.
  • Keep tests deterministic and specific; use pytest.approx() for floating-point assertions with reasonable tolerances.
  • Store the test prompts and generated tests in your repository as a reusable starting point for TDD experiments.
Further reading and references:

Watch Video