Drive Code With TDD
Purpose
Grow working, fully tested code in small verified steps, so that every line of production code exists because a test demanded it and the design emerges from how the code is used rather than from speculation.
When to use
- A new behavior, function or class is about to be written and its expected behavior can be stated.
- A bug must be fixed: start with a failing test that reproduces it.
- A team wants a worked TDD sequence for a concrete behavior (pairing, kata, teaching).
When not to use
- The code already exists and only needs tests. Use
unit-test-writing.
- The behavior itself is unknown and must be explored first; spike, then use
spike-report and restart with TDD.
- A larger story needs planning across components. Use
implement-from-story, which can apply this skill per task.
Inputs
Required:
- The behavior to build, as rules, examples or acceptance criteria.
Optional, improves quality:
- Language and test framework, existing code the behavior plugs into, coding conventions.
If the behavior is too vague to write a first failing test, ask one focused question at a time (at most 5 in a batch) about the rule. Treat any rule you fill in yourself as [ASSUMPTION] and list it before the first cycle.
Process
- Write a test list: concrete examples of the behavior, from the simplest degenerate case (empty, null, zero) through the core rule to edge and error cases. Mark examples you inferred as
[ASSUMPTION]. The list is a living backlog; add new ideas to it instead of coding them.
- Red: pick the next example that teaches the most with the least code. Write one test for it, using the API you wish existed, in Arrange-Act-Assert form.
- Run it and watch it fail for the right reason: an assertion failure on the expected value, not a compile error, missing import or wrong setup. If it passes unexpectedly, the behavior already exists or the test is wrong; investigate before continuing.
- Green: write the minimal production code to pass, including fake-it (returning a constant) when that is the simplest step. Do not add behavior no failing test demands.
- Run all tests; all must be green. If a previous test breaks, fix the code, not the test, unless the test was wrong.
- Refactor on green only: remove duplication between test and code, improve names, extract functions, generalize a faked constant once a second example forces it (triangulation). Tests stay green after every change; refactor test code too.
- Repeat steps 2-6 for the next item. Keep each cycle to a few minutes of work; if a step grows large, revert to green and pick a smaller example.
- When a bug surfaces, add a test that reproduces it first, see it fail, then fix.
- Stop when the test list is empty and the acceptance criteria are covered; review the final design and the test names as a readable specification.
- Report the cycle log, the final code and tests, and the assumptions made.
- If the goal continues, suggest
test-gap-finder to probe for missed cases, refactoring for larger structural cleanup, or implement-from-story to continue the surrounding story.
Output format
# TDD: <behavior>
Assumptions: <list or none>
## Test List
- [x] <example> → <expected>
- [ ] <example> [ASSUMPTION]
## Cycle Log
| # | Red: test | Fails because | Green: minimal change | Refactor |
|---|---|---|---|---|
| 1 | empty password rejected | returns true (stub) | `if empty return false` | – |
## Final Code
<production code>
## Final Tests
<test code>
Quality checklist
Common pitfalls
- Writing several tests up front and then all the code; the feedback loop and emergent design are lost. One test at a time.
- Skipping the red check; a test that never failed may be asserting nothing. Always see it fail.
- Skipping refactoring because "it is green"; duplication piles up and the code rots in small steps.
- Starting with the hardest example; the first test should be trivial so the API and setup are validated cheaply.
Example
Input: "Password validator: min 12 chars, at least one digit and one symbol, must not contain the user's email."
Weak cycle: one test with all four rules, then the full validator in one go.
Strong cycles (excerpt):
- Red:
empty_password_is_rejected fails (stub returns valid). Green: return invalid when empty.
- Red:
11_chars_with_digit_and_symbol_is_rejected. Green: length check < 12. Refactor: extract MIN_LENGTH.
- Red:
12_chars_without_digit_is_rejected. Green: digit check.
- Red:
password_containing_email_case_insensitive_is_rejected [ASSUMPTION: case-insensitive; confirm with security].
1---2name: tdd-cycle3description: "Drives a behavior into code with strict red-green-refactor cycles: a test list ordered from simplest to hardest, one failing test at a time that is seen failing for the right reason, the minimal code to pass, and refactoring only on green, with no production code written without a failing test. Use when someone wants to build a feature, function or bug fix test-first, asks for TDD steps, or wants to practice or demonstrate TDD on a concrete behavior."4license: MIT5metadata:6 source: https://skillmd.com/skills/muratdincer/tdd-cycle7 version: "1.0.0"8 language: en9 category: 05-engineering10 role: developer11 area: testing12 title: "Drive code with TDD"13 related: "unit-test-writing, implement-from-story, refactoring, acceptance-criteria, test-gap-finder"14 prompt: "Let's build a password strength validator with TDD: min 12 chars, at least one digit and one symbol, and it must reject the user's email."15---1617# Drive Code With TDD1819## Purpose20Grow working, fully tested code in small verified steps, so that every line of production code exists because a test demanded it and the design emerges from how the code is used rather than from speculation.2122## When to use23- A new behavior, function or class is about to be written and its expected behavior can be stated.24- A bug must be fixed: start with a failing test that reproduces it.25- A team wants a worked TDD sequence for a concrete behavior (pairing, kata, teaching).2627## When not to use28- The code already exists and only needs tests. Use `unit-test-writing`.29- The behavior itself is unknown and must be explored first; spike, then use `spike-report` and restart with TDD.30- A larger story needs planning across components. Use `implement-from-story`, which can apply this skill per task.3132## Inputs33Required:34- The behavior to build, as rules, examples or acceptance criteria.3536Optional, improves quality:37- Language and test framework, existing code the behavior plugs into, coding conventions.3839If the behavior is too vague to write a first failing test, ask one focused question at a time (at most 5 in a batch) about the rule. Treat any rule you fill in yourself as `[ASSUMPTION]` and list it before the first cycle.4041## Process421. Write a test list: concrete examples of the behavior, from the simplest degenerate case (empty, null, zero) through the core rule to edge and error cases. Mark examples you inferred as `[ASSUMPTION]`. The list is a living backlog; add new ideas to it instead of coding them.432. Red: pick the next example that teaches the most with the least code. Write one test for it, using the API you wish existed, in Arrange-Act-Assert form.443. Run it and watch it fail for the right reason: an assertion failure on the expected value, not a compile error, missing import or wrong setup. If it passes unexpectedly, the behavior already exists or the test is wrong; investigate before continuing.454. Green: write the minimal production code to pass, including fake-it (returning a constant) when that is the simplest step. Do not add behavior no failing test demands.465. Run all tests; all must be green. If a previous test breaks, fix the code, not the test, unless the test was wrong.476. Refactor on green only: remove duplication between test and code, improve names, extract functions, generalize a faked constant once a second example forces it (triangulation). Tests stay green after every change; refactor test code too.487. Repeat steps 2-6 for the next item. Keep each cycle to a few minutes of work; if a step grows large, revert to green and pick a smaller example.498. When a bug surfaces, add a test that reproduces it first, see it fail, then fix.509. Stop when the test list is empty and the acceptance criteria are covered; review the final design and the test names as a readable specification.5110. Report the cycle log, the final code and tests, and the assumptions made.5211. If the goal continues, suggest `test-gap-finder` to probe for missed cases, `refactoring` for larger structural cleanup, or `implement-from-story` to continue the surrounding story.5354## Output format55```markdown56# TDD: <behavior>57Assumptions: <list or none>5859## Test List60- [x] <example> → <expected>61- [ ] <example> [ASSUMPTION]6263## Cycle Log64| # | Red: test | Fails because | Green: minimal change | Refactor |65|---|---|---|---|---|66| 1 | empty password rejected | returns true (stub) | `if empty return false` | – |6768## Final Code69<production code>7071## Final Tests72<test code>73```7475## Quality checklist76- [ ] No production code was written without a failing test that required it.77- [ ] Every red step states the failure and it is the expected assertion failure, not a compile or setup error.78- [ ] Each green step is the minimal change; no speculative generality was added.79- [ ] Refactoring happened only on green and all tests stayed green.80- [ ] The test list covers degenerate, core, edge and error cases, and inferred rules are labeled `[ASSUMPTION]`.81- [ ] All checks pass; if any fails, revise the output and re-run this checklist before answering.8283## Common pitfalls84- Writing several tests up front and then all the code; the feedback loop and emergent design are lost. One test at a time.85- Skipping the red check; a test that never failed may be asserting nothing. Always see it fail.86- Skipping refactoring because "it is green"; duplication piles up and the code rots in small steps.87- Starting with the hardest example; the first test should be trivial so the API and setup are validated cheaply.8889## Example90Input: "Password validator: min 12 chars, at least one digit and one symbol, must not contain the user's email."9192Weak cycle: one test with all four rules, then the full validator in one go.9394Strong cycles (excerpt):951. Red: `empty_password_is_rejected` fails (stub returns valid). Green: return invalid when empty.962. Red: `11_chars_with_digit_and_symbol_is_rejected`. Green: length check `< 12`. Refactor: extract `MIN_LENGTH`.973. Red: `12_chars_without_digit_is_rejected`. Green: digit check.984. Red: `password_containing_email_case_insensitive_is_rejected` `[ASSUMPTION: case-insensitive; confirm with security]`.