--- name: tdd-loop description: "Use when you add or change behavior in code, or fix a bug. Runs the red, green, refactor cycle with a failing test first." --- # TDD loop Use when you implement behavior. Follow the red-green-refactor cycle from [tdd/index.md](https://raw.githubusercontent.com/daniel-kindl/dkstack/v1/tdd/index.md). ## Steps 1. Write a failing test that describes the desired behavior. Confirm it fails for the right reason. 2. Write the smallest amount of production code that makes the test pass. 3. Confirm the test passes and existing tests still pass. 4. Refactor only while tests are green. 5. Repeat for the next behavior. ## Rules - In the refactor step, stay inside the task scope. Do not change unrelated code. Keep every existing test green. Do not limit the refactor to code that a test already executes. When no test checks a behavior that this refactor can change, write a test for that behavior first. That test must pass before you change the code. See [stewardship](https://raw.githubusercontent.com/daniel-kindl/dkstack/v1/architecture/stewardship.md). - Do not write production code before a failing test exists. - Tests must prove behavior, not implementation details. - Do not skip the failing step. - Keep each cycle small. See also [coding/testing.md](https://raw.githubusercontent.com/daniel-kindl/dkstack/v1/coding/testing.md).