> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Test Driven Development with Claude

> A demo of test-driven development using Claude Code to generate pytest tests and implement a Python unit converter for distance, volume, mass, and temperature

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.

| Function                      | Formula / Factor                   | Description                         |
| ----------------------------- | ---------------------------------- | ----------------------------------- |
| miles\_to\_kilometers(miles)  | km = miles \* 1.60934              | 1 mile = 1.60934 kilometers         |
| kilometers\_to\_miles(km)     | miles = km \* 0.621371             | 1 km ≈ 0.621371 miles               |
| gallons\_to\_liters(gallons)  | L = gallons \* 3.785411784         | 1 US gallon = 3.785411784 liters    |
| liters\_to\_gallons(liters)   | gal = liters \* 0.2641720523581484 | 1 L ≈ 0.2641720523581484 US gallons |
| pounds\_to\_kilograms(pounds) | kg = pounds \* 0.45359237          | 1 lb = 0.45359237 kg                |
| kilograms\_to\_pounds(kg)     | lb = kg \* 2.2046226218487757      | 1 kg ≈ 2.2046226218487757 lb        |
| fahrenheit\_to\_celsius(F)    | C = (F - 32) \* 5/9                | Fahrenheit → Celsius                |
| celsius\_to\_fahrenheit(C)    | F = C \* 9/5 + 32                  | Celsius → Fahrenheit                |

## 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):

```python theme={null}
# test_converter.py (representative header)
import pytest
from converter import (
    miles_to_kilometers,
    kilometers_to_miles,
    gallons_to_liters,
    liters_to_gallons,
    pounds_to_kilograms,
    kilograms_to_pounds,
    fahrenheit_to_celsius,
    celsius_to_fahrenheit
)

class TestDistanceConversions:
    """Test distance conversion functions"""
    # ... test methods ...

class TestVolumeConversions:
    """Test volume conversion functions"""
    # ... test methods ...

class TestMassConversions:
    """Test mass conversion functions"""
    # ... test methods ...

class TestTemperatureConversions:
    """Test temperature conversion functions"""
    # ... test methods ...
```

The test file was edited in VS Code; when saving the test file the editor prompted to write changes:

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4gvfMR9MneljS-Uy/images/Claude-Code-For-Beginners/Advanced-Features/Demo-Test-Driven-Development-with-Claude/vscode-pytest-unit-converter-save-prompt.jpg?fit=max&auto=format&n=4gvfMR9MneljS-Uy&q=85&s=09cb27a50dfc3ad0fa83372fafed251e" alt="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." width="1920" height="1080" data-path="images/Claude-Code-For-Beginners/Advanced-Features/Demo-Test-Driven-Development-with-Claude/vscode-pytest-unit-converter-save-prompt.jpg" />
</Frame>

## Running the tests (first run)

Before implementing converter.py, running pytest fails at collection:

```text theme={null}
$ python3 -m pytest -q
ImportError while importing test module 'test_converter.py'
E   ModuleNotFoundError: No module named 'converter'
```

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:

```python theme={null}
def _ensure_numeric(value):
    """Internal helper to validate numeric inputs."""
    if not isinstance(value, (int, float)):
        raise TypeError("Input was not a numeric value")


def miles_to_kilometers(miles):
    """Converts miles to kilometers."""
    _ensure_numeric(miles)
    return float(miles) * 1.60934


def kilometers_to_miles(km):
    """Converts kilometers to miles."""
    _ensure_numeric(km)
    return float(km) * 0.621371


def gallons_to_liters(gallons):
    """Converts US gallons to liters."""
    _ensure_numeric(gallons)
    # Precise US gallon to liter factor
    return float(gallons) * 3.785411784


def liters_to_gallons(liters):
    """Converts liters to US gallons."""
    _ensure_numeric(liters)
    return float(liters) * 0.2641720523581484


def pounds_to_kilograms(pounds):
    """Converts pounds to kilograms."""
    _ensure_numeric(pounds)
    return float(pounds) * 0.45359237


def kilograms_to_pounds(kg):
    """Converts kilograms to pounds."""
    _ensure_numeric(kg)
    return float(kg) * 2.2046226218487757


def fahrenheit_to_celsius(fahrenheit):
    """Converts degrees Fahrenheit to Celsius."""
    _ensure_numeric(fahrenheit)
    return (float(fahrenheit) - 32.0) * (5.0 / 9.0)


def celsius_to_fahrenheit(celsius):
    """Converts degrees Celsius to Fahrenheit."""
    _ensure_numeric(celsius)
    return float(celsius) * (9.0 / 5.0) + 32.0
```

## 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:

```text theme={null}
$ python3 -m pytest -q
........................................  # all tests pass (representative)
40 passed in 0.12s
```

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

## 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:

* [Claude Code course](https://learn.kodekloud.com/user/courses/claude-code-for-beginners)
* [pytest documentation](https://docs.pytest.org/)
* [Python numbers — official docs](https://docs.python.org/3/library/stdtypes.html#numeric-types)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/claude-code-for-beginners/module/a295c914-f61e-47bb-8adc-7a3145745aa6/lesson/afd1bb19-60e7-4184-b98b-45e624967de4" />
</CardGroup>
