GitScrum / Docs
All Best Practices

Test-Driven Development Workflow | Red-Green-Refactor

Implement TDD with the red-green-refactor cycle. GitScrum tracks TDD adoption metrics and measures impact on bug rates and velocity.

10 min read

Tests drive design. GitScrum helps teams track TDD adoption and measure the impact of test-first practices on quality and velocity.

TDD Fundamentals

The Red-Green-Refactor Cycle

TDD CYCLE:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚ THE CYCLE:                                                  β”‚
β”‚ ──────────                                                  β”‚
β”‚                                                             β”‚
β”‚       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚       β”‚                                     β”‚              β”‚
β”‚       β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚              β”‚
β”‚       β”‚    β”‚  RED   β”‚ Write failing test    β”‚              β”‚
β”‚       β”‚    β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜                       β”‚              β”‚
β”‚       β”‚        β”‚                            β”‚              β”‚
β”‚       β”‚        β–Ό                            β”‚              β”‚
β”‚       β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚              β”‚
β”‚       β”‚    β”‚ GREEN  β”‚ Make it pass          β”‚              β”‚
β”‚       β”‚    β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ (minimal code)        β”‚              β”‚
β”‚       β”‚        β”‚                            β”‚              β”‚
β”‚       β”‚        β–Ό                            β”‚              β”‚
β”‚       β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚              β”‚
β”‚       └─────REFACTORβ”‚ Improve design        β”‚              β”‚
β”‚            β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ (tests still pass)    β”‚              β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ STEP 1 - RED:                                               β”‚
β”‚ Write a test for behavior that doesn't exist yet          β”‚
β”‚ Run it β†’ It fails (proves test works)                     β”‚
β”‚                                                             β”‚
β”‚ STEP 2 - GREEN:                                             β”‚
β”‚ Write just enough code to pass the test                   β”‚
β”‚ Don't over-engineer, don't optimize                       β”‚
β”‚ Just make it work                                          β”‚
β”‚                                                             β”‚
β”‚ STEP 3 - REFACTOR:                                          β”‚
β”‚ Now improve the code                                       β”‚
β”‚ Remove duplication, clarify names, improve design         β”‚
β”‚ Tests ensure you didn't break anything                    β”‚
β”‚                                                             β”‚
β”‚ REPEAT for each small piece of behavior                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Example Workflow

TDD in Practice

TDD EXAMPLE:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚ FEATURE: Calculate order total with discount               β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ STEP 1 - RED (Write failing test):                         β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ test('calculates total with 10% discount', () => {     β”‚β”‚
β”‚ β”‚   const order = new Order([                            β”‚β”‚
β”‚ β”‚     { price: 100 },                                    β”‚β”‚
β”‚ β”‚     { price: 50 }                                      β”‚β”‚
β”‚ β”‚   ]);                                                  β”‚β”‚
β”‚ β”‚   order.applyDiscount(0.1);                            β”‚β”‚
β”‚ β”‚   expect(order.total()).toBe(135);                     β”‚β”‚
β”‚ β”‚ });                                                     β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ RUN β†’ ❌ FAIL (Order doesn't exist)                    β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                                                             β”‚
β”‚ STEP 2 - GREEN (Make it pass):                             β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ class Order {                                          β”‚β”‚
β”‚ β”‚   constructor(items) {                                 β”‚β”‚
β”‚ β”‚     this.items = items;                                β”‚β”‚
β”‚ β”‚     this.discount = 0;                                 β”‚β”‚
β”‚ β”‚   }                                                     β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚   applyDiscount(rate) {                                β”‚β”‚
β”‚ β”‚     this.discount = rate;                              β”‚β”‚
β”‚ β”‚   }                                                     β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚   total() {                                            β”‚β”‚
β”‚ β”‚     const subtotal = this.items.reduce(               β”‚β”‚
β”‚ β”‚       (sum, item) => sum + item.price, 0              β”‚β”‚
β”‚ β”‚     );                                                 β”‚β”‚
β”‚ β”‚     return subtotal * (1 - this.discount);            β”‚β”‚
β”‚ β”‚   }                                                     β”‚β”‚
β”‚ β”‚ }                                                       β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ RUN β†’ βœ… PASS                                           β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                                                             β”‚
β”‚ STEP 3 - REFACTOR (Improve):                               β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ β€’ Extract subtotal calculation                         β”‚β”‚
β”‚ β”‚ β€’ Add validation                                       β”‚β”‚
β”‚ β”‚ β€’ Improve naming                                       β”‚β”‚
β”‚ β”‚ β€’ Run tests β†’ Still passing βœ…                         β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                                                             β”‚
β”‚ NEXT: Write another test for next behavior               β”‚
β”‚ (e.g., maximum discount, negative prices, etc.)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Benefits

Why TDD Works

TDD BENEFITS:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚ BETTER DESIGN:                                              β”‚
β”‚ ──────────────                                              β”‚
β”‚ Writing tests first forces you to think about:            β”‚
β”‚ β€’ How code will be used (API design)                      β”‚
β”‚ β€’ What the code should do (requirements)                  β”‚
β”‚ β€’ How to make it testable (loose coupling)                β”‚
β”‚                                                             β”‚
β”‚ Result: Cleaner, more modular code                        β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ CONFIDENCE:                                                 β”‚
β”‚ ───────────                                                 β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ WITHOUT TESTS:                                          β”‚β”‚
β”‚ β”‚ "Does this change break something?"                    β”‚β”‚
β”‚ β”‚ β†’ Manual testing, uncertainty, fear                    β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ WITH TDD:                                                β”‚β”‚
β”‚ β”‚ "Does this change break something?"                    β”‚β”‚
β”‚ β”‚ β†’ Run tests β†’ Know in seconds                          β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ DOCUMENTATION:                                              β”‚
β”‚ ──────────────                                              β”‚
β”‚ Tests document what code should do                        β”‚
β”‚ Executable documentation that can't go stale              β”‚
β”‚ New developers read tests to understand behavior          β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ LESS DEBUGGING:                                             β”‚
β”‚ ───────────────                                             β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ WRITE CODE FIRST:                                       β”‚β”‚
β”‚ β”‚ Write β†’ Test manually β†’ Find bugs β†’ Debug β†’ Fix       β”‚β”‚
β”‚ β”‚ Time: 2 hours coding + 3 hours debugging = 5 hours    β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ TDD:                                                     β”‚β”‚
β”‚ β”‚ Test β†’ Code β†’ Pass β†’ Repeat                            β”‚β”‚
β”‚ β”‚ Time: 3 hours (includes tests) + 30 min debug         β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ Bugs caught immediately, not hours later              β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Test Types

What to Test

TESTING PYRAMID WITH TDD:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚           /\                                                β”‚
β”‚          /  \      E2E Tests                               β”‚
β”‚         / UI \     (Few, slow, broad)                      β”‚
β”‚        /──────\                                             β”‚
β”‚       /        \   Integration Tests                       β”‚
β”‚      / Service  \  (Some, medium speed)                    β”‚
β”‚     /────────────\                                          β”‚
β”‚    /              \  Unit Tests                            β”‚
β”‚   /     Unit       \ (Many, fast, focused)                 β”‚
β”‚  /──────────────────\                                       β”‚
β”‚                                                             β”‚
β”‚ TDD FOCUS:                                                  β”‚
β”‚ ──────────                                                  β”‚
β”‚ Primarily unit tests                                       β”‚
β”‚ Fast feedback loop                                         β”‚
β”‚ Test one thing at a time                                  β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ WHAT TO TEST IN TDD:                                       β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ βœ… BUSINESS LOGIC:                                       β”‚β”‚
β”‚ β”‚ β€’ Calculations                                         β”‚β”‚
β”‚ β”‚ β€’ Validation rules                                     β”‚β”‚
β”‚ β”‚ β€’ State transitions                                    β”‚β”‚
β”‚ β”‚ β€’ Domain behavior                                      β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ ⚠️ INTEGRATION POINTS:                                  β”‚β”‚
β”‚ β”‚ β€’ API contracts                                        β”‚β”‚
β”‚ β”‚ β€’ Database queries (integration tests)                β”‚β”‚
β”‚ β”‚ β€’ External service calls (mocked in unit tests)       β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ ❌ DON'T TDD:                                            β”‚β”‚
β”‚ β”‚ β€’ Framework code (already tested)                     β”‚β”‚
β”‚ β”‚ β€’ Simple getters/setters                              β”‚β”‚
β”‚ β”‚ β€’ UI layout (use other testing)                       β”‚β”‚
β”‚ β”‚ β€’ Throwaway prototypes                                β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Team Adoption

Implementing TDD

ADOPTING TDD:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚ PHASE 1: LEARN (Week 1-2)                                  β”‚
β”‚ ─────────────────────────                                   β”‚
β”‚ β€’ TDD kata/workshop                                       β”‚
β”‚ β€’ Practice on toy problems                                β”‚
β”‚ β€’ Pair with experienced TDD practitioner                  β”‚
β”‚ β€’ Read/watch TDD resources                                β”‚
β”‚                                                             β”‚
β”‚ PHASE 2: NEW CODE (Week 3-4)                               β”‚
β”‚ ────────────────────────────                                β”‚
β”‚ β€’ Apply TDD to new features                               β”‚
β”‚ β€’ Start with simple stories                               β”‚
β”‚ β€’ Pair programming recommended                            β”‚
β”‚ β€’ Expect slower at first                                  β”‚
β”‚                                                             β”‚
β”‚ PHASE 3: EXPAND (Month 2+)                                 β”‚
β”‚ ──────────────────────────                                  β”‚
β”‚ β€’ More complex features                                   β”‚
β”‚ β€’ Add tests when modifying existing code                  β”‚
β”‚ β€’ Develop team testing patterns                           β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ COMMON CHALLENGES:                                          β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ "It's slower!"                                          β”‚β”‚
β”‚ β”‚ β†’ Initially yes. Track time saved in debugging.       β”‚β”‚
β”‚ β”‚   Long-term velocity increases.                       β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ "I don't know what test to write"                      β”‚β”‚
β”‚ β”‚ β†’ Start with the simplest behavior.                   β”‚β”‚
β”‚ β”‚   What's the most basic thing it should do?           β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ "How do I test this complex thing?"                    β”‚β”‚
β”‚ β”‚ β†’ If it's hard to test, design might need work.       β”‚β”‚
β”‚ β”‚   TDD pushes you toward better design.                β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ "We have too much legacy code"                         β”‚β”‚
β”‚ β”‚ β†’ TDD new code. Add tests when touching old code.     β”‚β”‚
β”‚ β”‚   Gradual improvement over time.                      β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Best Practices

TDD Guidelines

TDD BEST PRACTICES:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚ TEST NAMING:                                                β”‚
β”‚ ────────────                                                β”‚
β”‚ ❌ test1(), testCalculate()                                β”‚
β”‚                                                             β”‚
β”‚ βœ… 'should calculate total with tax'                       β”‚
β”‚ βœ… 'returns error when email invalid'                      β”‚
β”‚ βœ… 'applies discount only to eligible items'               β”‚
β”‚                                                             β”‚
β”‚ Name describes expected behavior                          β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ ONE ASSERTION PER TEST:                                    β”‚
β”‚ ────────────────────────                                    β”‚
β”‚ ❌                                                          β”‚
β”‚ test('order works', () => {                               β”‚
β”‚   expect(order.total()).toBe(100);                        β”‚
β”‚   expect(order.itemCount()).toBe(5);                      β”‚
β”‚   expect(order.isValid()).toBe(true);                     β”‚
β”‚ });                                                        β”‚
β”‚                                                             β”‚
β”‚ βœ…                                                          β”‚
β”‚ test('calculates correct total', () => {...});            β”‚
β”‚ test('counts items correctly', () => {...});              β”‚
β”‚ test('validates order', () => {...});                     β”‚
β”‚                                                             β”‚
β”‚ Easier to identify failures                               β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ SMALL STEPS:                                                β”‚
β”‚ ────────────                                                β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ DON'T: Write complex test requiring 100 lines of code β”‚β”‚
β”‚ β”‚ DO: Write test requiring 5-10 lines of code           β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ Smaller steps = faster feedback = fewer bugs          β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ DON'T SKIP REFACTOR:                                       β”‚
β”‚ ────────────────────                                        β”‚
β”‚ Green is not done!                                        β”‚
β”‚ Refactor before moving to next test                       β”‚
β”‚ Technical debt accumulates if you skip this               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Measuring TDD

Tracking Adoption

TDD METRICS:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                                             β”‚
β”‚ ADOPTION METRICS:                                           β”‚
β”‚ ─────────────────                                           β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ TDD ADOPTION - Q1 2025                                  β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ Stories using TDD:                                      β”‚β”‚
β”‚ β”‚ Jan:  20%  β–ˆβ–ˆβ–ˆβ–ˆ                                        β”‚β”‚
β”‚ β”‚ Feb:  45%  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ                                   β”‚β”‚
β”‚ β”‚ Mar:  68%  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ                               β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ Test coverage trend:                                    β”‚β”‚
β”‚ β”‚ Jan:  45%                                              β”‚β”‚
β”‚ β”‚ Feb:  58%                                              β”‚β”‚
β”‚ β”‚ Mar:  72%                                              β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                                                             β”‚
β”‚ ─────────────────────────────────────────────────────────── β”‚
β”‚                                                             β”‚
β”‚ QUALITY IMPACT:                                             β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚
β”‚ β”‚ BUGS IN PRODUCTION:                                     β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ Before TDD (2024):  8 bugs/sprint average             β”‚β”‚
β”‚ β”‚ After TDD (2025):   3 bugs/sprint average  (-63%)     β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ REWORK TIME:                                            β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β”‚ Before: 25% of sprint on bug fixes                    β”‚β”‚
β”‚ β”‚ After:  10% of sprint on bug fixes  (-60%)            β”‚β”‚
β”‚ β”‚                                                         β”‚β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”‚
β”‚                                                             β”‚
β”‚ VELOCITY:                                                   β”‚
β”‚ ─────────                                                   β”‚
β”‚ Short-term: May dip 10-20% during learning               β”‚
β”‚ Long-term: Often increases as debugging decreases         β”‚
β”‚                                                             β”‚
β”‚ Track both to show the investment pays off               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Related Solutions