Step 3: TDD with AI — Keeping You in the Driver’s Seat
Use AI to accelerate Test-Driven Development (TDD) without surrendering design intent or engineering judgment.
Step 3: TDD with AI — Keeping You in the Driver’s Seat
Objective
Use AI to accelerate Test-Driven Development (TDD) without surrendering design intent or engineering judgment.
The goal is not to let AI write your tests blindly. The goal is to use AI as a thinking partner while you remain the architect of the code.
Learning Path
1. Re-establish the TDD Loop
Before introducing AI, anchor on the classic cycle:
-
Red – Write a failing test
-
Green – Write the simplest code to pass
-
Refactor – Improve design safely
AI should support this loop, not bypass it.
Key rule:
Tests define intent. AI assists implementation.
2. Use AI to Generate Test Ideas
AI is excellent at producing test scenarios you may not immediately think of.
Ask AI questions like:
Generate unit test scenarios for this function.
Include edge cases, boundary conditions, and failure cases.
Example function:
def calculate_discount(price, percentage):
return price * (percentage / 100)
Possible AI-generated scenarios:
-
Normal discount case
-
Zero discount
-
100% discount
-
Negative percentage
-
Very large price values
-
Rounding behavior
Your job is to evaluate which tests reflect real system behavior.
AI suggests.
You decide.
3. Write the Tests Yourself
Do not copy-paste AI-generated test code.
Instead:
-
Review the AI test ideas
-
Select the meaningful ones
-
Write the tests manually
This preserves:
-
understanding
-
design clarity
-
debugging ability
Example:
def test_zero_discount():
assert calculate_discount(100, 0) == 0
4. Compare Your Tests With AI Suggestions
After writing your tests:
Ask AI:
Compare these unit tests with your earlier suggestions.
What cases might still be missing?
This is where AI shines as a coverage reviewer.
You may discover:
-
missing edge cases
-
input validation gaps
-
boundary conditions
5. Implement the Code to Pass Tests
Now return to the TDD loop.
Let the tests drive implementation.
AI can help with:
Prompt example:
Given these tests, suggest a simple implementation that passes them.
Do not add features not required by the tests.
6. Use AI for Safe Refactoring
Once tests pass, AI can help identify design improvements.
Ask:
Refactor this code while preserving behavior verified by the tests.
Focus on readability and simplicity.
Your safety net:
The test suite.
If tests pass, refactoring is safe.
Exercise
Goal
Practice using AI to expand test coverage while maintaining developer control.
Step 1 — Pick a Small Function
Choose something simple:
-
string parser
-
calculation function
-
validation logic
-
utility method
Step 2 — Ask AI for Test Cases
Example prompt:
Generate unit test cases for this function.
Include edge cases and failure scenarios.
Step 3 — Write Tests Yourself
Do not copy the AI output.
Instead:
-
read the suggestions
-
select meaningful ones
-
write tests manually
Step 4 — Compare Gaps
Ask AI:
Compare my tests with the earlier suggestions.
What important cases might still be missing?
Step 5 — Expand Coverage
Add the missing cases you agree with.
Your final test suite should reflect:
-
real requirements
-
edge conditions
-
error behavior
Key Principle
AI improves test discovery.
Developers maintain design ownership.
A useful mental model:
| Role |
Responsibility |
| Developer |
Defines intent and architecture |
| Tests |
Protect behavior |
| AI |
Suggests cases and improvements |
You stay in the driver’s seat.