Overview
Want to master Test-Driven Development through practical examples? This by-example guide teaches TDD through annotated code examples organized by complexity level.
What Is TDD By-Example Learning?
TDD by-example learning is a code-first approach where you learn through practical implementations of the Red-Green-Refactor cycle rather than narrative explanations. Each example shows:
- Failing tests first - Red phase demonstrating test-first thinking
- Minimal implementation - Green phase making tests pass with simplest code
- Refactoring - Improving design while keeping tests green
- Production patterns - Proven approaches from industry leaders
This approach is ideal for developers who want to master test-first development through hands-on practice.
Learning Path
The TDD by-example tutorial guides you through examples organized into three progressive levels, from basic unit testing to enterprise-scale TDD implementations.
Coverage Philosophy
This by-example guide provides practical coverage of TDD through annotated examples. The focus is on implementing TDD workflows, not just theory.
What's Covered
- TDD fundamentals - Red-Green-Refactor cycle, test-first thinking
- Testing patterns - Assertions, fixtures, test organization
- Test doubles - Mocks, stubs, fakes, spies
- Advanced techniques - Asynchronous testing, property-based testing, mutation testing
- Production patterns - TDD with databases, web apps, microservices
- Legacy code - Characterization tests, refactoring strategies
- Enterprise TDD - Test architecture, scaling patterns, performance
What's NOT Covered
- BDD and acceptance testing (see BDD tutorial for stakeholder collaboration)
- Manual testing techniques (see testing fundamentals)
- Framework documentation beyond common patterns
Prerequisites
- Programming experience in at least one language (Java, JavaScript, Python, Go, or similar)
- Basic understanding of functions, classes, and control flow
- Familiarity with testing concepts (assertions, test runners)
Structure of Each Example
Every example follows a consistent format:
- Brief Explanation: What TDD concept the example demonstrates
- Test Code (Red): Failing test showing requirements
- Implementation (Green): Minimal code to pass the test
- Refactored Version: Improved design preserving behavior
- Key Takeaway: The core TDD principle to retain
This structure provides the complete TDD cycle in every example, reinforcing the rhythm of test-first development.
Examples by Level
Beginner (Examples 1–30)
- Example 1: Your First Test (Hello World TDD)
- Example 2: Red-Green-Refactor Cycle
- Example 3: Testing Primitive Types (Numbers)
- Example 4: Testing Strings
- Example 5: Testing Booleans and Truthiness
- Example 6: Testing Arrays - Basic Operations
- Example 7: Testing Objects - Property Access
- Example 8: Test Fixtures - Setup and Teardown
- Example 9: Single Responsibility Principle in Tests
- Example 10: Testing Edge Cases - Null and Undefined
- Example 11: Testing Boundaries - Numbers
- Example 12: Testing Error Conditions
- Example 13: Arrange-Act-Assert (AAA) Pattern
- Example 14: Given-When-Then Test Structure
- Example 15: Test Organization - Describe Blocks
- Example 16: Test Naming Conventions
- Example 17: DRY Principle in Tests
- Example 18: Testing Pure Functions
- Example 19: Testing with Multiple Assertions (Same Concept)
- Example 20: Test-First Thinking Exercise
- Example 21: TDD Workflow Demonstration
- Example 22: Common Beginner Mistake - Testing Implementation
- Example 23: Testing Function Return Values
- Example 24: Testing Side Effects (State Changes)
- Example 25: Testing Output Messages
- Example 26: Testing with Simple Assertions
- Example 27: Basic Refactoring with Tests
- Example 28: Testing Collections - Filtering
- Example 29: Testing Transformations (Map)
- Example 30: Testing Aggregation (Reduce)
Intermediate (Examples 31–58)
- Example 31: Introduction to Test Doubles - Stubs
- Example 32: Test Doubles - Mocks
- Example 33: Test Doubles - Spies
- Example 34: Test Doubles - Fakes
- Example 35: Dependency Injection for Testability
- Example 36: Testing Promises - Basic Resolution
- Example 37: Testing Async/Await Patterns
- Example 38: Testing Callbacks
- Example 39: Testing Timers and Delays
- Example 40: Testing HTTP Requests - Mocking Fetch
- Example 41: Testing with In-Memory Databases
- Example 42: Property-Based Testing Introduction
- Example 43: Mutation Testing Concepts
- Example 44: Test Coverage Analysis
- Example 45: TDD with Express.js Routes
- Example 46: TDD with React Components
- Example 47: Testing Event-Driven Code
- Example 48: Testing State Machines
- Example 49: Parameterized Tests (test.each)
- Example 50: Snapshot Testing Use Cases
- Example 51: Testing File I/O Operations
- Example 52: Testing with Environment Variables
- Example 53: CI/CD Integration for TDD
- Example 54: Test Performance Optimization
- Example 55: Flaky Test Detection and Fixes
- Example 56: Test Data Builders Pattern
- Example 57: Object Mother Pattern
- Example 58: London vs Chicago TDD Schools
Advanced (Examples 59–85)
- Example 59: Characterization Tests for Legacy Code
- Example 60: Approval Testing for Complex Outputs
- Example 61: Working with Seams in Untestable Code
- Example 62: Dependency Breaking Techniques
- Example 63: TDD for Microservices - Service Isolation
- Example 64: Contract Testing for Microservices
- Example 65: Testing Distributed Systems - Eventual Consistency
- Example 66: Testing Event Sourcing Systems
- Example 67: Testing CQRS Patterns
- Example 68: TDD in Polyglot Environments
- Example 69: Performance-Sensitive TDD
- Example 70: Security Testing with TDD
- Example 71: TDD Anti-Patterns - Testing Implementation Details
- Example 72: Test-Induced Design Damage
- Example 73: Scaling TDD Across Teams
- Example 74: TDD Coaching and Mentoring
- Example 75: ROI Measurement for TDD
- Example 76: TDD in Regulated Industries
- Example 77: Compliance Testing Patterns
- Example 78: TDD for Machine Learning Systems
- Example 79: Testing AI/ML Model Behavior
- Example 80: Evolutionary Architecture with TDD
- Example 81: TDD in Continuous Deployment
- Example 82: Production Testing Patterns
- Example 83: Testing with Feature Flags
- Example 84: Synthetic Monitoring and Production Tests
- Example 85: TDD Anti-Pattern Recovery - Abandoned Test Suites
Last updated January 30, 2026