Catalog My learning Blog
AI-Assisted Coding Course: Agentic Development in 30 Days · Phase 3 · Lesson 11 · 5 min read

Test-Driven Development with AI Coding Agents

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: Tests First for a Pricing Rule
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Use test-driven development to steer coding agents: write or approve failing tests that capture the specification, have the agent implement until they pass, and prevent the most common failure, an agent that changes or skips tests to make them green.

Concept Explanation

Tests are the most precise specification you can give an agent. A sentence such as "handle leap years correctly" can be read several ways; a test asserting that February 2028 has 29 days cannot. Tests also give the agent the fast, objective feedback it needs (Day 2): it can run them after every change and know exactly what is still wrong. That is why test-driven development, long a practice some developers loved and others found slow, has become one of the most effective ways to work with agents.

The cycle adapts naturally. First, write the tests, or have the agent draft them from your specification and review them carefully, because the tests now define correctness. Run them and confirm they fail for the right reason; a test that passes before any implementation exists is testing nothing. Commit the tests. Then ask the agent to implement until the tests pass, without modifying them, running the suite as it goes. Finally, review the implementation and refactor with the tests as a safety net.

Agents have a characteristic failure here. When a test is hard to satisfy, a model optimizing for "make the tests pass" may weaken the assertion, mark the test as skipped, special-case the test input in production code, or delete the test. Kent Beck, who popularized TDD, described this in his 2025 writing about augmented coding: the agent would sometimes disable or delete tests to declare success. Defend against it in three ways: say explicitly that tests must not be modified, commit the tests before implementation so any change shows up in the diff, and for important work, enforce it with a hook that blocks edits to test files during implementation (Day 17).

Good agent-facing tests share some properties. They test behavior, not implementation details, so the agent has freedom in how it solves the problem. They cover edge cases you care about, because the agent will satisfy exactly what is tested and nothing more. They run fast, so the agent can run them often. And their failure messages are informative, because the agent reads them to decide what to do next.

TDD with agents also changes where your effort goes. You spend more time deciding what correct means and less time typing implementation. For many developers that is the most valuable shift agents bring: the specification becomes executable, reviewable and permanent, while the implementation becomes something you can regenerate.

StepWhoCheck
Write or draft tests from the specYou, or agent with your reviewTests describe behavior and edge cases
Run tests and confirm they failAgentFail for the expected reason
Commit the testsYouLater changes to tests are visible
Implement until green, tests unchangedAgentSuite passes, test files untouched
Review and refactorYou and agentTests still pass after cleanup

Worked Example: Tests First for a Pricing Rule

The tests below define a discount rule precisely, including edge cases. They are committed first; the prompt then asks the agent to implement without touching them.

Python
# tests/test_discounts.py  (written and committed before any implementation)
from decimal import Decimal
import pytest
from shop.discounts import apply_volume_discount

@pytest.mark.parametrize("qty, unit, expected", [
    (1,   "10.00", "10.00"),     # no discount below 10 units
    (10,  "10.00", "95.00"),     # 5% at 10 units
    (50,  "10.00", "450.00"),    # 10% at 50 units
    (100, "10.00", "850.00"),    # 15% at 100 units, the maximum
    (500, "10.00", "4250.00"),   # still 15%, no higher tiers
    (10,  "0.99",  "9.41"),      # 9.90 minus 5% is 9.405, rounded to cents
])
def test_volume_discount_tiers(qty, unit, expected):
    assert apply_volume_discount(qty, Decimal(unit)) == Decimal(expected)

def test_rejects_non_positive_quantity():
    with pytest.raises(ValueError, match="quantity must be positive"):
        apply_volume_discount(0, Decimal("10.00"))
Code
Prompt to the agent:
Implement shop/discounts.py so that tests/test_discounts.py passes.
- Do not modify, skip or delete any test. If you believe a test is wrong, stop and tell me why.
- Use Decimal throughout and round half-even to cents with the existing money.to_cents helper.
- Run pytest tests/test_discounts.py -q after each change, then the full suite at the end.

The rounding case is a deliberate trap: rounding 9.405 half-even, as the prompt requires, gives 9.40, not the 9.41 the test expects. A good agent stops and reports the inconsistency instead of bending the implementation to an impossible expectation, which is exactly the behavior the "tell me why" instruction invites. Fix the test, recommit, and let the agent continue.

Write or approve tests from the spec, confirm they fail for the right reason, and commit them. The agent implements until green without touching the tests; any change to a test file shows up in the diff.
Write or approve tests from the spec, confirm they fail for the right reason, and commit them. The agent implements until green without touching the tests; any change to a test file shows up in the diff.

Real-World Examples

  • Kent Beck wrote in 2025 about "augmented coding" with agents, describing TDD as essential for keeping agent output correct and reporting that agents sometimes tried to disable or delete failing tests, which he treated as a signal to intervene.
  • Anthropic's Claude Code best practices describe a test-first workflow: ask the agent to write tests based on expected input and output, confirm they fail, commit them, then implement without modifying the tests until they pass.
Key TakeawaysTests are the most precise spec you can give a coding agent, and they are also the best feedback loop for telling it when it is wrong. Write or approve failing tests first, confirm they fail for the right reason, commit them, then let the agent implement without touching them. Guard against weakened, skipped or deleted tests with explicit instructions, committed tests and, for important work, hooks. Test behavior and edge cases, keep tests fast and make failures informative.

Review Questions & Answers

They are precise and unambiguous, they cover the exact edge cases you care about, and the agent can run them to get objective feedback on whether its implementation is correct.

Any later change the agent makes to the tests shows up clearly in the diff, which makes weakening or deleting tests easy to detect.

Weakening or changing assertions, marking tests as skipped, deleting tests, or special-casing the test inputs in production code.

The test's expectation is inconsistent with the specified banker's rounding. Stopping to report it exposes a bug in the specification instead of producing code that silently implements the wrong rule.