Test-Driven Development with GitScrum | TDD Workflow
Apply red-green-refactor TDD cycles within agile sprints. GitScrum tracks test-first subtasks as part of story definition of done.
9 min read
TDD isn't just about testing - it's about design and confidence. GitScrum supports TDD workflows with task tracking that reflects the red-green-refactor cycle.
TDD Fundamentals
The TDD Cycle
RED-GREEN-REFACTOR CYCLE:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β ββββββββββββββ β
β β RED β β Write failing test β
β β (Failing β "What should the code do?" β
β β Test) β β
β βββββββ¬βββββββ β
β β β
β βΌ β
β ββββββββββββββ β
β β GREEN β β Write minimum code to pass β
β β (Passing β "Make it work, nothing more" β
β β Test) β β
β βββββββ¬βββββββ β
β β β
β βΌ β
β ββββββββββββββ β
β β REFACTOR β β Improve without breaking tests β
β β (Clean β "Make it clean" β
β β Code) β β
β βββββββ¬βββββββ β
β β β
β ββββββββββββ Repeat β
β β
β CYCLE TIME: Minutes, not hours β
β β
β KEY RULES: β
β β’ Never write code without a failing test β
β β’ Write the minimum code to pass β
β β’ Refactor only when tests are green β
β β’ Run tests frequently β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
TDD Benefits
WHY TDD WORKS:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β DESIGN BENEFITS: β
β β’ Forces you to think about interface first β
β β’ Leads to modular, testable code β
β β’ Catches design problems early β
β β’ Results in simpler designs β
β β
β QUALITY BENEFITS: β
β β’ Bugs caught immediately β
β β’ High test coverage naturally β
β β’ Regression protection β
β β’ Confidence to refactor β
β β
β PRODUCTIVITY BENEFITS: β
β β’ Less time debugging β
β β’ Faster feedback β
β β’ Documentation through tests β
β β’ Easier onboarding β
β β
β PSYCHOLOGICAL BENEFITS: β
β β’ Small wins (green tests) β
β β’ Confidence in code β
β β’ Safe to change β
β β’ Clear progress β
β β
β WITHOUT TDD: β
β Write code β Test later β Find bugs β Debug β Fix β Repeatβ
β β
β WITH TDD: β
β Write test β Write code β Test passes β Refactor β Done β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
TDD in Practice
Example Workflow
TDD EXAMPLE - ADD TO CART:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β STORY: "As a user, I can add items to my cart" β
β β
β TDD CYCLE 1: β
β β
β RED: Write test β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β test('adds item to empty cart', () => { ββ
β β const cart = new Cart(); ββ
β β cart.add(item); ββ
β β expect(cart.items.length).toBe(1); ββ
β β }); ββ
β β // Result: β FAILS - Cart doesn't exist ββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β GREEN: Write minimum code β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β class Cart { ββ
β β items = []; ββ
β β add(item) { this.items.push(item); } ββ
β β } ββ
β β // Result: β
PASSES ββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β REFACTOR: (Nothing to refactor yet) β
β β
β TDD CYCLE 2: β
β β
β RED: Next test β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β test('increases quantity if item exists', () => { ββ
β β const cart = new Cart(); ββ
β β cart.add(item); ββ
β β cart.add(item); ββ
β β expect(cart.items.length).toBe(1); ββ
β β expect(cart.items[0].quantity).toBe(2); ββ
β β }); ββ
β β // Result: β FAILS - no quantity handling ββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β GREEN: Add quantity logic β
β REFACTOR: Extract findItem method β
β β
β Continue until all requirements covered... β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Test Types
TDD TEST PYRAMID:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β /\ β
β / \ β
β / E2E\ Few, slow, expensive β
β /ββββββ\ β
β / \ β
β /Integration\ Some, medium speed β
β /ββββββββββββββ\ β
β / \ β
β / Unit Tests \ Many, fast, cheap β
β /ββββββββββββββββββββ\ β
β β
β UNIT TESTS (TDD Focus): β
β β’ Test single functions/methods β
β β’ No external dependencies β
β β’ Milliseconds to run β
β β’ Write many of these β
β β
β INTEGRATION TESTS: β
β β’ Test components together β
β β’ May use database, API β
β β’ Seconds to run β
β β’ Write fewer of these β
β β
β E2E TESTS: β
β β’ Test full user flows β
β β’ Real browser, real services β
β β’ Minutes to run β
β β’ Write few, critical paths only β
β β
β TDD PRIMARILY: Unit tests β
β ALSO USEFUL: Integration tests for key flows β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
TDD in GitScrum Workflow
Story with TDD
TDD INTEGRATED IN STORY FLOW:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β STORY: Add to Cart Functionality β
β β
β STATUS: In Progress β
β β
β DEFINITION OF DONE: β
β β All acceptance criteria met β
β β Unit tests written and passing β
β β Code reviewed β
β β Integrated and deployed β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β SUBTASKS (TDD Approach): β
β β
β β Write tests for add to empty cart β
β β Implement add to empty cart β
β β Write tests for quantity increment β
β β Implement quantity increment β
β β Write tests for max quantity β
β β Implement max quantity β
β β Write tests for out of stock β
β β Implement out of stock β
β β Refactor and cleanup β
β β
β TEST COVERAGE: β
β Cart module: 94% coverage β
β All tests: β
47 passing β
β β
β NOTE: Each test-implement pair is a mini red-green cycle β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Tracking TDD Work
TDD IN TASK TRACKING:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β OPTION 1: Tests as part of story β
β β
β Story: Add to cart β
β βββ Subtasks: β
β βββ Tests for happy path β
β βββ Implementation β
β βββ Tests for edge cases β
β βββ Refactoring β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β OPTION 2: Tests implicit in definition of done β
β β
β Story: Add to cart β
β Definition of Done: β
β β’ Unit tests with 80%+ coverage β
β β’ All tests passing β
β β’ Edge cases tested β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β DO NOT: β
β β
β β "Testing" as separate story after development β
β β "Write tests" as a sprint task after code β
β β Skip tests when under time pressure β
β β
β Tests are PART of development, not separate β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Common Challenges
Starting with TDD
TDD ADOPTION TIPS:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β START SMALL: β
β β’ Start with new code, not legacy β
β β’ Start with simple features β
β β’ Pair with experienced TDD developer β
β β
β LEARN THE RHYTHM: β
β β’ Resist urge to write more code than needed β
β β’ Keep cycles short (minutes) β
β β’ Celebrate green tests β
β β
β COMMON MISTAKES: β
β β
β β Tests too big β
β β Break into smaller tests β
β β
β β Writing implementation first β
β β Stop, delete, write test first β
β β
β β Not refactoring β
β β Schedule refactor time, it's essential β
β β
β β Skipping when rushed β
β β Rushing without tests creates more rush later β
β β
β TEAM ADOPTION: β
β β
β β’ Include in definition of done β
β β’ Review tests in code review β
β β’ Share testing patterns β
β β’ Celebrate test-first culture β
β β’ Pair on complex features β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
When TDD Is Hard
CHALLENGING TDD SCENARIOS:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β UI COMPONENTS: β
β Challenge: Testing DOM, visuals β
β Approach: Test behavior, not appearance β
β Tools: Testing Library, Jest β
β Tip: Separate logic from rendering β
β β
β DATABASE/EXTERNAL SERVICES: β
β Challenge: Slow, stateful tests β
β Approach: Mock external dependencies β
β Tools: Mock libraries, test doubles β
β Tip: Test integration separately β
β β
β LEGACY CODE: β
β Challenge: Code not designed for testing β
β Approach: Characterization tests first β
β Tools: Approval tests β
β Tip: Refactor to testable design gradually β
β β
β UNCERTAIN REQUIREMENTS: β
β Challenge: Don't know what to test β
β Approach: Spike first, then TDD β
β Tip: Throw away spike code, rewrite with tests β
β β
β PERFORMANCE-CRITICAL: β
β Challenge: Need to optimize β
β Approach: TDD for correctness, then optimize β
β Tip: Tests protect while optimizing β
β β
β "If it's hard to test, the design might need work" β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ